🇮🇳
🇮🇳
Limited-Time Offer!Get 20% OFF on all live courses
Enroll Now
P
PrakalpanaLive online tech training
Interview Prep⏱️ 14 min read📅 Jan 6

10 Java Interview Questions That 90% Candidates Fail (With Answers)

RK
Rajesh KumarSenior Java Architect
📑 Contents (12 sections)

📌Why These Questions Are Tricky

These aren't textbook questions. They test deep understanding of Java internals, and 90% of candidates struggle with them.

📌Question 1: String Pool Behavior

String s1 = "hello";
String s2 = "hello";
String s3 = new String("hello");
System.out.println(s1 == s2); // ?
System.out.println(s1 == s3); // ?
System.out.println(s1.equals(s3)); // ?

Answer: true, false, true

Why: String literals go to String Pool. new String() creates object in heap.

📌Question 2: HashMap Collision

"What happens when two keys have the same hashCode in HashMap?"

Answer:

  • 1Both go to same bucket
  • 2Stored as linked list (or tree if >8 elements)
  • 3equals() used to find exact key
  • 4This is why equals/hashCode contract matters
  • 📌Question 3: Immutability

    "Make this class immutable:"

    class Person {
    private String name;
    private List<String> hobbies;
    // getters and setters
    }

    Answer:

    final class Person {
    private final String name;
    private final List<String> hobbies;
    public Person(String name, List<String> hobbies) {
    this.name = name;
    this.hobbies = new ArrayList<>(hobbies); // defensive copy
    }
    public List<String> getHobbies() {
    return new ArrayList<>(hobbies); // return copy
    }
    }

    📌Question 4: Memory Leak

    "How can memory leak occur in Java?"

    Answer:

  • 1Unclosed resources (streams, connections)
  • 2Static collections holding references
  • 3Improper equals/hashCode (can't find in HashMap)
  • 4Inner class holding outer class reference
  • 5ThreadLocal not cleaned
  • 📌Question 5: Volatile vs Synchronized

    "When would you use volatile instead of synchronized?"

    Answer:

  • Volatile: Single variable, simple read/write, visibility guarantee
  • Synchronized: Multiple operations, compound actions, mutual exclusion
  • Example: volatile for flags, synchronized for counters.

    📌Question 6: ConcurrentModificationException

    "How to avoid ConcurrentModificationException?"

    // This throws exception
    for (String s : list) {
    if (condition) list.remove(s);
    }
    // Solutions:
    // 1. Use Iterator.remove()
    // 2. Use CopyOnWriteArrayList
    // 3. Collect and remove after loop
    // 4. Use removeIf()
    list.removeIf(s -> condition);

    📌Question 7: Singleton Breaking

    "How can singleton pattern be broken?"

    Answer:

  • 1Reflection: setAccessible(true) on constructor
  • 2Serialization: Multiple instances during deserialization
  • 3Cloning: clone() creates new instance
  • Prevention: Use enum singleton (Joshua Bloch recommended).

    📌Question 8: JVM Memory Areas

    "Explain JVM memory structure"

    Answer:

  • Heap: Objects, GC managed
  • Stack: Method calls, local variables (per thread)
  • Metaspace: Class metadata (replaced PermGen in Java 8)
  • Native Stack: JNI calls
  • 📌Question 9: Generics Erasure

    "Why does this fail?"

    List<Integer> list = new ArrayList<>();
    if (list instanceof List<Integer>) { } // Compile error!

    Answer: Type erasure—generics removed at compile time. At runtime, it's just List.

    📌Question 10: CompletableFuture

    "Chain three async operations with error handling"

    CompletableFuture.supplyAsync(() -> fetchUser())
    .thenApplyAsync(user -> fetchOrders(user))
    .thenApplyAsync(orders -> calculateTotal(orders))
    .exceptionally(ex -> handleError(ex))
    .join();

    📌Prepare Better

    Our Java course covers all these concepts with hands-on coding exercises. Don't just memorize—understand.

    RK

    Written by

    Rajesh Kumar

    Senior Java Architect

    🚀 Master Interview Prep

    Join 5000+ developers

    Explore Courses →