Java Programming — Collections Framework
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 100 questions in Collections Framework
In Java 2024, if you need a collection that automatically removes entries based on garbage collection patterns, which would you choose?
A WeakHashMap
B IdentityHashMap
C EnumMap
D ConcurrentHashMap
Correct Answer:  A. WeakHashMap
EXPLANATION

WeakHashMap uses weak references for keys. When a key is no longer referenced elsewhere, it becomes eligible for garbage collection and is automatically removed from the map.

Take Test
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
Advertisement
Consider this code: List list = new CopyOnWriteArrayList(); list.add("A"); list.add("B"); Iterator itr = list.iterator(); list.add("C"); System.out.println(itr.next());
A Throws ConcurrentModificationException
B Prints 'A'
C Prints 'C'
D Throws NoSuchElementException
Correct Answer:  B. Prints 'A'
EXPLANATION

CopyOnWriteArrayList creates a snapshot for iterators, so modifications after iterator creation don't affect the iteration. Iterator sees only original elements: A and B.

Take Test
What will happen when you execute: Map map = new HashMap(); map.put("key", null); System.out.println(map.get("key"));
A Throws NullPointerException
B Prints 'null'
C Prints 'key'
D Throws IllegalArgumentException
Correct Answer:  B. Prints 'null'
EXPLANATION

HashMap allows null values. The code stores null as the value for key 'key' and retrieves it successfully, printing null.

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
What will be the output of the following code? Set set = new TreeSet(); set.add(5); set.add(2); set.add(8); set.add(2); System.out.println(set.size());
A 3
B 4
C 2
D 5
Correct Answer:  A. 3
EXPLANATION

TreeSet does not allow duplicates. Adding 2 twice means it is stored only once. Final set contains {2, 5, 8}, so size is 3.

Take Test
Which of the following collections maintains insertion order while allowing fast random access?
A ArrayList
B HashSet
C TreeSet
D LinkedHashMap
Correct Answer:  A. ArrayList
EXPLANATION

ArrayList maintains insertion order and provides O(1) random access via index. LinkedHashMap maintains insertion order but is a Map, not a List.

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