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.102Medium
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.103Medium
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.104Medium
Which interface provides sorted ordering in Collections Framework?
Answer: B
SortedSet and SortedMap interfaces provide sorted ordering. TreeSet implements SortedSet, TreeMap implements SortedMap.
Q.105Medium
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.
Advertisement
Q.106Medium
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.
Q.107Medium
What will be the result of executing this code?
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.add(5);
pq.add(3);
pq.add(7);
System.out.println(pq.peek());
Answer: B
PriorityQueue orders elements based on their natural ordering (min-heap by default). peek() returns 3, the minimum element.
Q.108Medium
Consider the following code. What is the behavior?
Collection<String> col = new ArrayList<>();
col.add("Java");
Iterator<String> it = col.iterator();
while(it.hasNext()) {
String s = it.next();
col.remove(s);
}
Answer: B
Modifying collection directly while iterating throws ConcurrentModificationException. Use iterator.remove() instead.
Q.109Medium
What is the output of the following code?
List<String> list = Arrays.asList("A", "B", "C");
list.add("D");
System.out.println(list.size());
Answer: C
Arrays.asList() returns a fixed-size list backed by array. add() operation is not supported.
Q.110Medium
What is the time complexity of contains() operation in HashSet?
Answer: A
HashSet uses hash table internally. Average case is O(1), worst case O(n) when hash collisions occur.
Q.111Medium
Which of the following is a fail-fast iterator behavior?
Answer: B
Fail-fast iterators throw ConcurrentModificationException when collection is modified during iteration.
Q.112Medium
Consider a scenario where you need fast random access and frequent insertions in the middle. Which collection is most suitable?
Answer: B
LinkedList provides O(1) insertions/deletions at any position. ArrayList is O(n) for middle insertions due to shifting.
Q.113Medium
What does the removeIf() method in Collection interface do?
Answer: B
removeIf(Predicate) removes all elements that satisfy the given predicate condition (Java 8+).
Q.114Medium
What is the time complexity of add() operation in ArrayList?
Answer: A
ArrayList.add() has O(1) amortized time complexity. When capacity is exceeded, resizing takes O(n).
Q.115Medium
Which collection maintains insertion order and also provides thread-safety?
Answer: D
CopyOnWriteArrayList maintains insertion order and is thread-safe. It creates a copy of the array on modification.
Q.116Medium
What is the difference between Iterator and ListIterator?
Answer: A
ListIterator extends Iterator and provides bidirectional traversal using previous() and next() methods.
Q.117Medium
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.118Medium
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.119Medium
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.120Medium
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.