Java Programming — Collections Framework
Java OOP, collections, multithreading
49 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 49 questions in Collections Framework
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());
A 10 then 20
B 10 then 10
C 30 then 20
D 20 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].

Take Test
Which collection interface should be used if you need both key-value pairing and need to iterate in the order of keys' natural ordering?
A HashMap
B TreeMap
C Hashtable
D WeakHashMap
Correct Answer:  B. TreeMap
EXPLANATION

TreeMap implements NavigableMap and SortedMap, storing key-value pairs in sorted order of keys using a Red-Black tree structure.

Take Test
What is the time complexity of get() operation on a LinkedHashMap with n elements?
A O(1) average case
B O(n)
C O(log n)
D O(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.

Take Test
Which of the following statements is true about PriorityQueue?
A Elements are stored in insertion order
B It implements the sorted set interface
C Elements are ordered based on their natural ordering or a comparator
D It 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.

Take Test
Which collection would you use if you need to maintain a sorted order of elements and also need O(log n) insertion/deletion time complexity?
A ArrayList
B LinkedList
C TreeSet
D Vector
Correct Answer:  C. TreeSet
EXPLANATION

TreeSet maintains elements in sorted order using a Red-Black tree internally, providing O(log n) for add, remove, and contains operations.

Take Test
Advertisement
Which collection interface guarantees that elements are stored in a sorted order with no duplicates?
A List
B Queue
C SortedSet
D Collection
Correct Answer:  C. SortedSet
EXPLANATION

SortedSet interface guarantees sorted order and no duplicates. TreeSet is the primary implementation of SortedSet.

Take Test
Consider a TreeMap with Integer keys. What will headMap(10) return?
A All entries with keys greater than 10
B All entries with keys less than 10
C All entries with keys less than or equal to 10
D All entries in reverse order
Correct Answer:  B. All entries with keys less than 10
EXPLANATION

TreeMap.headMap(10) returns a view of all entries with keys strictly less than 10. It's exclusive of the provided key.

Take Test
What is the purpose of the fail-fast iterator in Collections?
A To speed up iteration
B To detect concurrent modifications during iteration
C To remove elements faster
D To sort elements during iteration
Correct Answer:  B. To detect concurrent modifications during iteration
EXPLANATION

Fail-fast iterators throw ConcurrentModificationException if the collection is modified during iteration (except via iterator.remove()).

Take Test
Which collection implementation is best for frequent insertions and deletions at both ends?
A ArrayList
B LinkedList
C Vector
D Stack
Correct Answer:  B. LinkedList
EXPLANATION

LinkedList provides O(1) operations for adding/removing at both ends via addFirst(), addLast(), removeFirst(), removeLast().

Take Test
What happens when you add a null key to a HashMap?
A NullPointerException is thrown
B The null key is stored at index 0
C The null key is ignored
D The insertion fails silently
Correct Answer:  B. The null key is stored at index 0
EXPLANATION

HashMap allows one null key which is handled specially and stored at index 0. TreeMap does not allow null keys.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips