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.222Medium
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.223Easy
Which collection class implements NavigableMap interface?
Answer: B
TreeMap implements NavigableMap which provides methods like higherKey(), lowerKey(), floorEntry(), etc.
Q.224Medium
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.225Easy
In the context of streams and collections, which method returns a Sequential Stream in Java 8+?
Answer: B
collection.stream() returns a sequential stream, while parallelStream() returns a parallel stream.
Advertisement
Q.226Medium
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.227Medium
Which of the following is a fail-fast iterator behavior?
Answer: B
Fail-fast iterators throw ConcurrentModificationException when collection is modified during iteration.
Q.228Medium
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.229Medium
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.230Hard
What is the behavior of get() method in LinkedHashMap with accessOrder=true?
Answer: B
LinkedHashMap with accessOrder=true maintains access-order. get() moves the element to the end (LRU cache behavior).
Q.231Hard
Consider implementing a Comparator for custom sorting in reverse order. Which approach is correct?
List<Integer> list = Arrays.asList(5, 2, 8, 1);
Answer: D
All three approaches work correctly for reverse sorting. Options A, B, and C are all valid Java code.
Q.232Easy
Which Collection method was introduced in Java 9 to create immutable collections?
Answer: B
Java 9 introduced factory methods like List.of(), Set.of(), Map.of() for creating immutable collections.
Q.233Hard
In a multi-threaded environment, if you need a thread-safe list that allows concurrent reads, which is optimal?
Answer: B
CopyOnWriteArrayList is optimal for read-heavy concurrent operations. It creates copy on write, allowing safe concurrent reads.
Q.234Easy
Which of the following Collection interfaces does NOT support duplicate elements?
Answer: A
Set interface does not allow duplicate elements, while List, Queue, and Collection support duplicates.
Q.235Easy
What is the default initial capacity of a HashMap in Java?
Answer: B
HashMap has a default initial capacity of 16 and a load factor of 0.75.
Q.236Easy
Which collection class is synchronized by default?
Answer: C
Hashtable is a legacy synchronized collection class. HashMap, ArrayList, and TreeSet are not synchronized by default.
Q.237Easy
What does the poll() method return when called on an empty Queue?
Answer: B
poll() returns null if the queue is empty, while remove() throws NoSuchElementException.
Q.238Easy
Which of the following correctly represents the hierarchy of Collection framework?
Answer: B
The correct hierarchy is Iterable (interface) -> Collection -> List/Set/Queue/Deque interfaces.
Q.239Medium
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.240Medium
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.