Skip to main content
Java Basics
CHAPTER 28 Beginner

Java Interview Preparation

Updated: May 17, 2026
5 min read

# CHAPTER 28

Java Interview Preparation

1. Introduction

This chapter is your Java interview survival kit. We've compiled the 50 most frequently asked Java interview questions across Core Java, OOP, Collections, and Exception Handling — plus 20 coding challenges that interviewers love to ask.

2. Core Java Interview Questions

Q1: What is the difference between JDK, JRE, and JVM? JDK = Development Kit (compiler + tools + JRE). JRE = Runtime Environment (JVM + libraries). JVM = Virtual Machine that executes bytecode.

Q2: Why is Java platform-independent? Java compiles to bytecode (.class files) which runs on any JVM, regardless of the underlying OS.

Q3: What is the difference between == and .equals()? == compares memory addresses (reference equality). .equals() compares content (value equality). For Strings, always use .equals().

Q4: What is autoboxing and unboxing? Autoboxing: primitive → wrapper (intInteger). Unboxing: wrapper → primitive (Integerint).

Q5: What is the String Pool? A special memory area in the Heap where String literals are stored and reused to save memory.

Q6: Why are Strings immutable in Java? Security (passwords), thread safety, and performance (String Pool caching).

Q7: What is the difference between final, finally, and finalize()? final = constant/prevent override. finally = always-execute block. finalize() = garbage collection hook (deprecated).

Q8: Can we override a static method? No. Static methods are resolved at compile time (method hiding, not overriding).

Q9: What is the transient keyword? Marks a field to be excluded from serialization.

Q10: What is the difference between throw and throws? throw creates/throws an exception. throws declares that a method might throw an exception.

3. OOP Interview Questions

Q11: What are the four pillars of OOP? Encapsulation, Inheritance, Polymorphism, Abstraction.

Q12: What is the difference between an abstract class and an interface? Abstract class can have constructors, instance variables, and concrete methods. Interface defines a contract with abstract methods (Java 8+ allows default/static methods). A class can implement multiple interfaces but extend only one class.

Q13: What is the diamond problem? When a class inherits from two classes that have the same method, creating ambiguity. Java avoids this by not allowing multiple class inheritance.

Q14: What is method overloading vs overriding? Overloading: same name, different parameters (compile-time). Overriding: child redefines parent method (runtime).

Q15: What is the super keyword used for? Access parent class members (methods, fields, constructors) from a child class.

Q16: Can a constructor be inherited? No. Constructors are not inherited, but a child can call super() to invoke the parent constructor.

Q17: What is a Singleton pattern? A design pattern ensuring a class has only one instance. Achieved with a private constructor and a static getInstance() method.

Q18: What is the difference between composition and inheritance? Inheritance = IS-A (Dog IS-A Animal). Composition = HAS-A (Car HAS-A Engine). Prefer composition over inheritance.

4. Collections Interview Questions

Q19: ArrayList vs LinkedList? ArrayList: fast random access O(1), slow insert/delete O(n). LinkedList: slow access O(n), fast insert/delete O(1).

Q20: HashMap vs TreeMap vs LinkedHashMap? HashMap: O(1) lookup, no order. TreeMap: O(log n), sorted by key. LinkedHashMap: O(1), maintains insertion order.

Q21: How does HashMap work internally? Uses hashing. Key's hashCode() determines the bucket index. Collisions are resolved using linked lists (or trees in Java 8+).

Q22: What is the difference between HashSet and TreeSet? HashSet: O(1) operations, no order. TreeSet: O(log n), elements are sorted.

Q23: What is the fail-fast iterator? An iterator that throws ConcurrentModificationException if the collection is modified during iteration.

Q24: How to make a collection thread-safe? Use Collections.synchronizedList(), ConcurrentHashMap, or CopyOnWriteArrayList.

5. Exception Handling Questions

Q25: Checked vs Unchecked exceptions? Checked: must handle at compile time (IOException). Unchecked: runtime errors (NullPointerException).

Q26: Can we have try without catch? Yes, with finally or try-with-resources.

Q27: What is the order of catch blocks? Most specific exception first, most general last.

6. Top 20 Coding Challenges

C1: Reverse a String

java
123
public static String reverse(String s) {
    return new StringBuilder(s).reverse().toString();
}

C2: Check Palindrome

java
1234
public static boolean isPalindrome(String s) {
    String reversed = new StringBuilder(s).reverse().toString();
    return s.equals(reversed);
}

C3: Fibonacci Series

java
1234567
public static void fibonacci(int n) {
    int a = 0, b = 1;
    for (int i = 0; i < n; i++) {
        System.out.print(a + " ");
        int temp = a + b; a = b; b = temp;
    }
}

C4: Check Prime Number

java
1234567
public static boolean isPrime(int n) {
    if (n < 2) return false;
    for (int i = 2; i <= Math.sqrt(n); i++) {
        if (n % i == 0) return false;
    }
    return true;
}

C5: Find Largest in Array

java
12345
public static int findMax(int[] arr) {
    int max = arr[0];
    for (int n : arr) if (n > max) max = n;
    return max;
}

C6: Factorial

java
1234
public static long factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

C7: Count Vowels

java
1234
public static int countVowels(String s) {
    return (int) s.toLowerCase().chars()
        .filter(c -> "aeiou".indexOf(c) != -1).count();
}

C8: Remove Duplicates from Array

java
123
public static int[] removeDuplicates(int[] arr) {
    return Arrays.stream(arr).distinct().toArray();
}

C9: Check Anagram

java
123456
public static boolean isAnagram(String a, String b) {
    char[] c1 = a.toLowerCase().toCharArray();
    char[] c2 = b.toLowerCase().toCharArray();
    Arrays.sort(c1); Arrays.sort(c2);
    return Arrays.equals(c1, c2);
}

C10: FizzBuzz

java
123456
for (int i = 1; i <= 100; i++) {
    if (i % 15 == 0) System.out.println("FizzBuzz");
    else if (i % 3 == 0) System.out.println("Fizz");
    else if (i % 5 == 0) System.out.println("Buzz");
    else System.out.println(i);
}

C11: Swap Two Numbers Without Temp Variable

java
1
a = a ^ b; b = a ^ b; a = a ^ b;

C12: Check Armstrong Number

java
12345
public static boolean isArmstrong(int n) {
    int original = n, sum = 0, digits = String.valueOf(n).length();
    while (n > 0) { sum += Math.pow(n % 10, digits); n /= 10; }
    return sum == original;
}

C13: Binary Search

java
12345678910
public static int binarySearch(int[] arr, int target) {
    int low = 0, high = arr.length - 1;
    while (low <= high) {
        int mid = (low + high) / 2;
        if (arr[mid] == target) return mid;
        if (arr[mid] < target) low = mid + 1;
        else high = mid - 1;
    }
    return -1;
}

C14: Bubble Sort

java
12345
public static void bubbleSort(int[] arr) {
    for (int i = 0; i < arr.length - 1; i++)
        for (int j = 0; j < arr.length - i - 1; j++)
            if (arr[j] > arr[j + 1]) { int t = arr[j]; arr[j] = arr[j+1]; arr[j+1] = t; }
}

C15: Count Words in a String

java
12
String[] words = text.trim().split("\\s+");
System.out.println("Word count: " + words.length);

C16: Find Second Largest

java
12345
int max = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
for (int n : arr) {
    if (n > max) { second = max; max = n; }
    else if (n > second && n != max) second = n;
}

C17: String to Integer (Without parseInt)

java
12345
public static int stringToInt(String s) {
    int result = 0;
    for (char c : s.toCharArray()) result = result * 10 + (c - &#039;0&#039;);
    return result;
}

C18: Matrix Transpose

java
123
for (int i = 0; i < rows; i++)
    for (int j = 0; j < cols; j++)
        transposed[j][i] = matrix[i][j];

C19: Power Without Math.pow

java
12345
public static long power(int base, int exp) {
    long result = 1;
    for (int i = 0; i < exp; i++) result *= base;
    return result;
}

C20: Find Missing Number in Array (1 to n)

java
1234
int n = arr.length + 1;
int expectedSum = n * (n + 1) / 2;
int actualSum = Arrays.stream(arr).sum();
int missing = expectedSum - actualSum;

7. Summary

This chapter covered the most critical Java interview topics across Core Java, OOP, Collections, and Exception Handling. The 20 coding challenges cover the patterns interviewers test most frequently. Practice these until you can write them from memory.

8. Next Chapter Recommendation

In Chapter 29: Data Structures and Algorithms in Java, we'll implement essential data structures from scratch.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·