Consider a scenario: You need a data structure that maintains elements in sorted order and allows fast insertion/deletion. Which is optimal?
Answer: A
TreeSet maintains sorted order (O(log n) insertion/deletion) using a Red-Black tree structure.
Q.22Medium
What exception is thrown by Iterator.next() when there are no more elements?
Answer: A
Iterator.next() throws NoSuchElementException when hasNext() returns false and next() is called.
Q.23Medium
Which method of NavigableSet returns the greatest element strictly less than the given element?
Answer: B
lower() returns the greatest element < given element. floor() returns <= element.
Q.24Medium
What is the output of Collections.frequency(list, null) if list contains null elements?
Answer: A
Collections.frequency() correctly handles null values and returns the count of null elements in the collection.
Q.25Medium
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.
Advertisement
Q.26Medium
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.27Medium
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.28Medium
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.29Medium
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.
Q.30Medium
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.31Medium
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.32Medium
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.33Medium
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.34Medium
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.35Medium
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.36Medium
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.37Medium
What does Collections.unmodifiableList() return?
Answer: A
Collections.unmodifiableList() returns a read-only view of the list. Any modification attempts throw UnsupportedOperationException.
Q.38Medium
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.39Medium
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.40Medium
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.