๐Ÿ‡ฎ๐Ÿ‡ณ
๐Ÿ‡ฎ๐Ÿ‡ณ
Republic Day Special Offer!Get 20% OFF on all courses
Enroll Now
P
Prakalpana
๐Ÿ“šLearn
โ€ขCode Your Future
Interview Prepโฑ๏ธ 14 min read๐Ÿ“… Jan 6

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

RK
Rajesh Kumarโ€ขSenior 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 500+ developers

    Explore Courses โ†’
    Chat on WhatsApp