What is the time complexity of get() method in HashMap?
Answer: C
HashMap provides O(1) average time complexity for get() operation. In worst case with hash collisions, it can be O(n).
Q.202Easy
Which of the following collections maintains insertion order?
Answer: C
LinkedHashSet maintains insertion order using doubly-linked list. HashSet doesn't maintain order, TreeSet maintains sorted order, HashMap doesn't maintain insertion order.
Q.203Easy
What is the difference between ArrayList and LinkedList?
Answer: A
ArrayList is backed by a resizable array providing O(1) random access. LinkedList uses doubly-linked list providing O(1) insertion/deletion at ends but O(n) for random access.
Q.204Medium
Which method is used to remove an element from ArrayList while iterating?
Answer: B
Using remove() directly in loop causes ConcurrentModificationException. Iterator.remove() is the safe way to remove elements while iterating.
Q.205Medium
What is the load factor in HashMap? What is its default value?
Answer: B
Load factor is the ratio of size to capacity. Default load factor in HashMap is 0.75, which provides good balance between time and space complexity.
Advertisement
Q.206Easy
Which collection does NOT allow duplicate elements?
Answer: B
Set interface implementations like HashSet do not allow duplicates. Lists (ArrayList, LinkedList, Vector) allow duplicates.
Q.207Medium
What is the time complexity of add() operation in TreeSet?
Answer: B
TreeSet is backed by a TreeMap which uses Red-Black tree. Add operation requires O(log n) time for tree balancing.
Q.208Medium
Which of the following is thread-safe?
Answer: C
ConcurrentHashMap is thread-safe and uses bucket-level locking. HashMap, TreeMap, and LinkedHashMap are not thread-safe.
Q.209Medium
What does the Comparable interface do in Collections Framework?
Answer: A
Comparable interface provides compareTo() method to define natural ordering for objects. Used by TreeSet and TreeMap for sorting.
Q.210Medium
Which collection is best for frequent insertions and deletions in the middle?
Answer: B
LinkedList provides O(1) insertion/deletion at ends and O(n) at middle. ArrayList requires O(n) for middle insertions/deletions due to shifting.
Q.211Easy
What is the difference between TreeSet and HashSet in sorting?
Answer: A
TreeSet maintains elements in sorted order using Red-Black tree. HashSet is unordered and uses hash table for storage.
Q.212Hard
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.213Hard
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.214Medium
Given:
Map<String, Integer> map = new HashMap<>();
map.put("A", 10);
map.put("B", 20);
map.put("A", 30);
What will be the size of map?
Answer: B
HashMap stores unique keys. Putting same key 'A' again replaces the old value. Size remains 2 with keys A and B.
Q.215Medium
Which interface provides sorted ordering in Collections Framework?
Answer: B
SortedSet and SortedMap interfaces provide sorted ordering. TreeSet implements SortedSet, TreeMap implements SortedMap.
Q.216Hard
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.217Medium
Which collection class is synchronized and legacy?
Answer: C
Vector is a legacy synchronized collection similar to ArrayList. It is thread-safe but slower. ArrayList is preferred with Collections.synchronizedList() for synchronization.
Q.218Easy
Which method in Set collection prevents duplicate insertion by returning false?
Answer: A
The add() method in Set returns boolean - true if element is added, false if duplicate exists.
Q.219Easy
What is the output of LinkedHashMap iteration in the following code?
LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);
for(String key : map.keySet()) System.out.print(key);
Answer: A
LinkedHashMap maintains insertion order. Elements are iterated in the order they were inserted.
Q.220Medium
Which of the following statements about Queue interface is correct?
Answer: C
offer() returns false if element cannot be added, while add() throws exception. poll() returns null if empty.