No collection allows duplicate keys. When a duplicate key is added to a Map, it overwrites the previous value. HashSet and TreeSet don't allow duplicates.
Q.82Medium
What is the initial capacity of a HashMap in Java?
Answer: B
HashMap has an initial capacity of 16 and doubles its capacity when load factor (0.75) is reached.
Q.83Easy
Which method returns true if the map contains the specified value?
Answer: B
The containsValue() method checks if the map contains a specified value. containsKey() checks for keys.
Q.84Medium
What happens when you add a null key to a HashMap?
Answer: B
HashMap allows one null key which is handled specially and stored at index 0. TreeMap does not allow null keys.
Q.85Medium
Which collection implementation is best for frequent insertions and deletions at both ends?
Answer: B
LinkedList provides O(1) operations for adding/removing at both ends via addFirst(), addLast(), removeFirst(), removeLast().
Advertisement
Q.86Medium
What is the purpose of the fail-fast iterator in Collections?
Answer: B
Fail-fast iterators throw ConcurrentModificationException if the collection is modified during iteration (except via iterator.remove()).
Q.87Medium
Consider a TreeMap with Integer keys. What will headMap(10) return?
Answer: B
TreeMap.headMap(10) returns a view of all entries with keys strictly less than 10. It's exclusive of the provided key.
Q.88Hard
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.89Hard
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.
Q.90Medium
Which collection interface guarantees that elements are stored in a sorted order with no duplicates?
Answer: C
SortedSet interface guarantees sorted order and no duplicates. TreeSet is the primary implementation of SortedSet.
Q.91Easy
Which of the following collections maintains insertion order while allowing fast random access?
Answer: A
ArrayList maintains insertion order and provides O(1) random access via index. LinkedHashMap maintains insertion order but is a Map, not a List.
Q.92Easy
What will be the output of the following code? Set<Integer> set = new TreeSet<>(); set.add(5); set.add(2); set.add(8); set.add(2); System.out.println(set.size());
Answer: A
TreeSet does not allow duplicates. Adding 2 twice means it is stored only once. Final set contains {2, 5, 8}, so size is 3.
Q.93Medium
Which collection would you use if you need to maintain a sorted order of elements and also need O(log n) insertion/deletion time complexity?
Answer: C
TreeSet maintains elements in sorted order using a Red-Black tree internally, providing O(log n) for add, remove, and contains operations.
Q.94Easy
What will happen when you execute: Map<String, String> map = new HashMap<>(); map.put("key", null); System.out.println(map.get("key"));
Answer: B
HashMap allows null values. The code stores null as the value for key 'key' and retrieves it successfully, printing null.
Q.95Hard
Consider this code: List<String> list = new CopyOnWriteArrayList<>(); list.add("A"); list.add("B"); Iterator<String> itr = list.iterator(); list.add("C"); System.out.println(itr.next());
Answer: B
CopyOnWriteArrayList creates a snapshot for iterators, so modifications after iterator creation don't affect the iteration. Iterator sees only original elements: A and B.
Q.96Medium
Which of the following statements is true about PriorityQueue?
Answer: C
PriorityQueue is a heap-based implementation where elements are ordered based on their priority determined by natural ordering or a custom comparator provided during initialization.
Q.97Medium
What is the time complexity of get() operation on a LinkedHashMap with n elements?
Answer: A
LinkedHashMap extends HashMap and uses the same hash table for storage, maintaining O(1) average-case get operation. The doubly-linked list only maintains insertion order.
Q.98Medium
Which collection interface should be used if you need both key-value pairing and need to iterate in the order of keys' natural ordering?
Answer: B
TreeMap implements NavigableMap and SortedMap, storing key-value pairs in sorted order of keys using a Red-Black tree structure.
Q.99Medium
What will be printed? Queue<Integer> queue = new LinkedList<>(); queue.add(10); queue.add(20); queue.add(30); System.out.println(queue.poll()); System.out.println(queue.peek());
Answer: A
LinkedList implements Queue with FIFO behavior. poll() removes and returns 10. peek() returns 20 without removing it. Queue now contains [20, 30].
Q.100Hard
In Java 2024, if you need a collection that automatically removes entries based on garbage collection patterns, which would you choose?
Answer: A
WeakHashMap uses weak references for keys. When a key is no longer referenced elsewhere, it becomes eligible for garbage collection and is automatically removed from the map.