An open empty notebook on a white desk next to an iPhone and a MacBook

Top 20 Java Interview Questions — Intermediate Level

JAVA

For candidates who have mastered Phase 2 (OOP & Core APIs)

1. What is the difference between abstraction and encapsulation?

Answer:

  • Abstraction: Hides implementation details, shows only essential features (e.g., interfaces, abstract classes).

  • Encapsulation: Wraps data and behavior together; achieved using private fields and public getters/setters.

2. What are the main differences between interfaces and abstract classes?

Answer:

  • Interfaces can have only abstract methods (and default/static methods since Java 8), while abstract classes can have both abstract and concrete methods.

  • A class can implement multiple interfaces but only extend one class.

3. What is the difference between method overloading and overriding?

Answer:

  • Overloading: Same method name, different parameters (compile-time polymorphism).

  • Overriding: Subclass redefines a method of the parent class (runtime polymorphism).

4. What is the difference between ArrayList and LinkedList?

Answer:

  • ArrayList is backed by a dynamic array → fast random access.

  • LinkedList uses a doubly linked list → fast insertions/deletions but slower access.

5. Explain the difference between HashMap and TreeMap.

Answer:

  • HashMap: Unordered, allows one null key, O(1) average.

  • TreeMap: Sorted order, no null key, O(log n) operations.

6. How does HashMap work internally?

Answer:

  • Uses an array of buckets (linked lists/red-black trees since Java 8).

  • Key’s hashCode() determines bucket; collision resolved via chaining.

  • When load factor exceeds threshold, it resizes (rehashing).

7. What is a ConcurrentModificationException?

Answer:

Thrown when a collection is structurally modified while iterating (fail-fast behavior). To avoid, use Iterator.remove() or concurrent collections.

8. What is the difference between Comparable and Comparator?

Answer:

  • Comparable: Natural ordering (compareTo).

  • Comparator: Custom ordering (compare).

Collections.sort(list, Comparator.comparing(Person::getAge));

9. What are generics and why are they useful?

Answer:

Enable type safety at compile time and eliminate explicit casting.

List<String> names = new ArrayList<>();

10. What is autoboxing and unboxing?

Answer:

Automatic conversion between primitives and wrapper classes.

Integer x = 10; // autoboxing
int y = x; // unboxing

11. What is the difference between throw and throws?

Answer:

  • throw: Used to explicitly throw an exception.

  • throws: Declares exceptions a method might throw.

12. Explain the difference between checked and unchecked exceptions.

Answer:

  • Checked: Compile-time (e.g., IOException).

  • Unchecked: Runtime (e.g., NullPointerException).

13. What is the try-with-resources statement?

Answer: Automatically closes resources implementing AutoCloseable.

try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
// use br
}

14. What is immutability in Java?

Answer: Once created, object state cannot change. Example: String.

  • To make a class immutable: mark fields final, private, and avoid setters.

15. Explain the concept of StringBuilder vs StringBuffer.

Answer:

  • StringBuilder: Non-synchronized (faster).

  • StringBuffer: Synchronized (thread-safe).

16. What is the difference between shallow copy and deep copy?

Answer:

  • Shallow copy: Copies references (same nested objects).

  • Deep copy: Clones all nested objects.

17. What are functional interfaces?

Answer:

Interfaces with one abstract method. Example: Runnable, Callable, Comparator.

Custom:

@FunctionalInterface
interface Greeting { void sayHello(); }

18. What are lambdas and how are they used?

Answer: Short syntax for anonymous functions.

list.forEach(x -> System.out.println(x));

19. What is the Optional class used for?

Answer: To avoid null checks.

Optional<String> name = Optional.ofNullable(user.getName());
name.ifPresent(System.out::println);

20. What is the Java Stream API?

Answer: Functional-style operations on collections.

List<Integer> squares = numbers.stream()
.filter(n -> n > 0)
.map(n -> n * n)
.toList();

🧠 Tip for Intermediate Developers

  • Practice reading JDK source (e.g., ArrayList, HashMap).

  • Solve problems using Streams & Lambdas.

  • Build small OOP projects (Library System, Employee Manager, etc.)