๐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:
๐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:
๐Question 5: Volatile vs Synchronized
"When would you use volatile instead of synchronized?"
Answer:
Example: volatile for flags, synchronized for counters.
๐Question 6: ConcurrentModificationException
"How to avoid ConcurrentModificationException?"
// This throws exceptionfor (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:
Prevention: Use enum singleton (Joshua Bloch recommended).
๐Question 8: JVM Memory Areas
"Explain JVM memory structure"
Answer:
๐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.