What will be printed? Queue queue = new LinkedList(); queue.add(10); queue.add(20); queue.add(30); System.out.println(queue.poll()); System.out.println(queue.peek());
A10 then 20
B10 then 10
C30 then 20
D20 then 30
Correct Answer:
A. 10 then 20
EXPLANATION
LinkedList implements Queue with FIFO behavior. poll() removes and returns 10. peek() returns 20 without removing it. Queue now contains [20, 30].
What is the time complexity of get() operation on a LinkedHashMap with n elements?
AO(1) average case
BO(n)
CO(log n)
DO(n log n)
Correct Answer:
A. O(1) average case
EXPLANATION
LinkedHashMap extends HashMap and uses the same hash table for storage, maintaining O(1) average-case get operation. The doubly-linked list only maintains insertion order.
Which of the following statements is true about PriorityQueue?
AElements are stored in insertion order
BIt implements the sorted set interface
CElements are ordered based on their natural ordering or a comparator
DIt is thread-safe by default
Correct Answer:
C. Elements are ordered based on their natural ordering or a comparator
EXPLANATION
PriorityQueue is a heap-based implementation where elements are ordered based on their priority determined by natural ordering or a custom comparator provided during initialization.