What is the primary purpose of the Comparator interface in Collections Framework?
ATo compare two objects and return equality
BTo define custom ordering for sorting collections
CTo prevent duplicate elements
DTo enhance performance of HashMap
Correct Answer:
B. To define custom ordering for sorting collections
EXPLANATION
Comparator interface allows defining custom comparison logic for sorting. It provides compare(T o1, T o2) method to establish custom ordering independent of the object's natural order.
Which of these operations is guaranteed to be atomic in ConcurrentHashMap?
Aif(!map.containsKey(key)) map.put(key, value);
Bmap.putIfAbsent(key, value);
Cmap.get(key) followed by map.put(key, newValue);
Dmap.remove(key) followed by map.put(key, value);
Correct Answer:
B. map.putIfAbsent(key, value);
EXPLANATION
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.
What is the behavior of WeakHashMap when a key is no longer strongly referenced?
AThe entry remains in the map forever
BThe entry is eligible for garbage collection
CA NullPointerException is thrown
DThe key is automatically reset to null
Correct Answer:
B. The entry is eligible for garbage collection
EXPLANATION
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.
How does Java 8+ handle hash collisions in HashMap differently?
AUses double hashing
BConverts bucket linked list to Red-Black tree when size exceeds threshold
CIncreases hash table size automatically
DUses linear probing
Correct Answer:
B. Converts bucket linked list to Red-Black tree when size exceeds threshold
EXPLANATION
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)).
What is the advantage of using LinkedList over ArrayList for frequent insertions at the beginning?
ALinkedList has O(1) insertion at beginning, ArrayList has O(n)
BArrayList is always faster
CLinkedList uses less memory
DLinkedList supports null values while ArrayList doesn't
Correct Answer:
A. LinkedList has O(1) insertion at beginning, ArrayList has O(n)
EXPLANATION
LinkedList provides O(1) insertion at the beginning (head), while ArrayList requires O(n) time to shift elements. Both support nulls and LinkedList uses more memory due to node pointers.
In what scenario would you use a ConcurrentHashMap instead of HashMap?
AWhen you need faster performance
BWhen you need thread-safe operations without locking entire map
CWhen you need to store null values
DWhen you need to maintain insertion order
Correct Answer:
B. When you need thread-safe operations without locking entire map
EXPLANATION
ConcurrentHashMap uses bucket-level locking (segment locking in Java 7, node-level in Java 8+) allowing concurrent reads/writes, making it thread-safe without synchronizing the entire map.
Which Collections utility method creates an immutable empty list?
ACollections.emptyList()
BCollections.unmodifiableList(new ArrayList())
Cnew ArrayList()
DArrays.asList()
Correct Answer:
A. Collections.emptyList()
EXPLANATION
Collections.emptyList() returns an immutable, empty list. Option B also works but is more verbose. Option C creates mutable list, and Option D creates mutable list from array.