What is the time complexity of get() operation in TreeMap?
Answer: B
TreeMap is based on Red-Black tree, so get(), put(), and remove() operations have O(log n) time complexity due to tree traversal.
Q.122Medium
What is the difference between ArrayList and Vector?
Answer: B
Vector is a legacy synchronized collection, while ArrayList is unsynchronized but faster. For thread-safety with ArrayList, use Collections.synchronizedList().
Q.123Medium
Which method of PriorityQueue returns the element without removing it?
Answer: C
peek() returns the head element without removing it. poll() removes and returns, remove() throws exception if empty, and pop() is not a PriorityQueue method.
Q.124Medium
What exception is thrown when you try to add a null value to a TreeSet?
Answer: A
TreeSet uses comparator or natural ordering, which cannot handle null values, throwing NullPointerException during insertion.
Q.125Medium
Which Collections utility method creates an immutable empty list?
Answer: A
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.
Advertisement
Q.126Medium
In what scenario would you use a ConcurrentHashMap instead of HashMap?
Answer: B
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.
Q.127Medium
Consider: Set<Integer> set = new HashSet<>(Arrays.asList(1,2,3,2,1)); System.out.println(set.size()); What is the output?
Answer: B
HashSet removes duplicates. The list [1,2,3,2,1] is converted to a set containing only unique elements {1,2,3}, so size is 3.
Q.128Medium
Which method in NavigableMap returns a view of the map in descending order?
Answer: A
descendingMap() method of NavigableMap returns a reverse-ordered view. reverseMap() doesn't exist in Collections Framework.
Q.129Medium
What is the advantage of using LinkedList over ArrayList for frequent insertions at the beginning?
Answer: A
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.
Q.130Medium
Which method is used to remove all elements from a Collection that satisfy a given predicate?
Answer: C
The removeIf() method was introduced in Java 8 to remove elements based on a given Predicate condition.
Q.131Medium
What is the key difference between TreeSet and HashSet?
Answer: B
TreeSet implements SortedSet and maintains elements in sorted order with O(log n) operations. HashSet has O(1) average operations but no order guarantee.
Q.132Medium
Which interface should be implemented to define custom sorting in Collections.sort()?
Answer: A
The Comparable interface allows objects to define their natural ordering by implementing compareTo() method.
Q.133Medium
What does Collections.unmodifiableList() return?
Answer: A
Collections.unmodifiableList() returns a read-only view of the list. Any modification attempts throw UnsupportedOperationException.
Q.134Medium
Which collection allows duplicate keys in Java?
Answer: D
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.135Medium
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.136Medium
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.137Medium
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().
Q.138Medium
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.139Medium
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.140Medium
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.