Which method throws ConcurrentModificationException when collection is modified during iteration?
Answer: C
Both Iterator and enhanced for loop use fail-fast mechanism and throw ConcurrentModificationException if collection is modified during iteration.
Q.2Hard
What is the initial capacity and growth strategy of ArrayList?
Answer: C
ArrayList starts with capacity 10. Growth factor is 50% + 1 in older versions, and 1.5x from Java 9 onwards.
Q.3Hard
What is the worst-case time complexity of HashMap.get() when hash collisions occur?
Answer: C
In worst case with all collisions stored in linked list, get() becomes O(n). From Java 8, if collisions exceed threshold, linked list converts to balanced tree, making it O(log n).
Q.4Hard
What is the behavior of get() method in LinkedHashMap with accessOrder=true?
Answer: B
LinkedHashMap with accessOrder=true maintains access-order. get() moves the element to the end (LRU cache behavior).
Q.5Hard
Consider implementing a Comparator for custom sorting in reverse order. Which approach is correct?
List<Integer> list = Arrays.asList(5, 2, 8, 1);
Answer: D
All three approaches work correctly for reverse sorting. Options A, B, and C are all valid Java code.
Advertisement
Q.6Hard
In a multi-threaded environment, if you need a thread-safe list that allows concurrent reads, which is optimal?
Answer: B
CopyOnWriteArrayList is optimal for read-heavy concurrent operations. It creates copy on write, allowing safe concurrent reads.
Q.7Hard
In Java 16+, what feature improved collection performance by allowing value-based classes?
Answer: A
Records (Java 14+) provide compact syntax for data carriers used with collections, improving performance and readability.
Q.8Hard
What is the behavior when concurrent modification occurs during iteration in ConcurrentHashMap vs HashMap?
What does the containsAll() method of Collection return for an empty collection?
Answer: A
containsAll() returns true for empty collections because mathematically, all elements of an empty set are contained in any set (vacuous truth).
Q.15Hard
How does Java 8+ handle hash collisions in HashMap differently?
Answer: B
Java 8+ introduced tree nodes: when a bucket's linked list size exceeds TREEIFY_THRESHOLD (8), it converts to a Red-Black tree for better performance (O(log n) vs O(n)).
Q.16Hard
What is the behavior of WeakHashMap when a key is no longer strongly referenced?
Answer: B
WeakHashMap uses weak references for keys. When a key is no longer strongly referenced elsewhere, it becomes eligible for garbage collection, and its entry is removed from the map.
Q.17Hard
Which of these operations is guaranteed to be atomic in ConcurrentHashMap?
Answer: B
putIfAbsent() is an atomic operation. Options A and C-D are compound operations that are not atomic and can have race conditions between checks and modifications.
Q.18Hard
In Java 2024-25, which enhancement improved record support in collections?
Answer: B
Java 21+ enhanced sealed classes and pattern matching, allowing records to be used with pattern matching in collection operations and switch statements for type-safe data handling.
Q.19Hard
Which method in Collections can create a thread-safe Map from a non-thread-safe Map?
Answer: A
Collections.synchronizedMap() wraps a Map with synchronized access. However, iteration still requires external synchronization.
Q.20Hard
What is the main advantage of EnumSet over HashSet when working with Enum constants?
Answer: B
EnumSet is specialized for Enum constants and uses bit vectors internally, providing O(1) operations with minimal memory overhead.