Java Programming — Collections Framework
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 11–20 of 100 questions in Collections Framework
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
What is the main advantage of EnumSet over HashSet when working with Enum constants?
A EnumSet is thread-safe
B EnumSet uses bit vectors internally for better performance
C EnumSet allows null elements
D EnumSet maintains insertion order
Correct Answer:  B. EnumSet uses bit vectors internally for better performance
EXPLANATION

EnumSet is specialized for Enum constants and uses bit vectors internally, providing O(1) operations with minimal memory overhead.

Take Test
Which method in Collections can create a thread-safe Map from a non-thread-safe Map?
A Collections.synchronizedMap()
B Collections.synchronizedList()
C new ConcurrentHashMap()
D Collections.unmodifiableMap()
Correct Answer:  A. Collections.synchronizedMap()
EXPLANATION

Collections.synchronizedMap() wraps a Map with synchronized access. However, iteration still requires external synchronization.

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
Advertisement
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
Which method returns true if the map contains the specified value?
A containsKey(Object key)
B containsValue(Object value)
C contains(Object value)
D hasValue(Object value)
Correct Answer:  B. containsValue(Object value)
EXPLANATION

The containsValue() method checks if the map contains a specified value. containsKey() checks for keys.

Take Test
What is the initial capacity of a HashMap in Java?
A 8
B 16
C 32
D 64
Correct Answer:  B. 16
EXPLANATION

HashMap has an initial capacity of 16 and doubles its capacity when load factor (0.75) is reached.

Take Test
Which collection allows duplicate keys in Java?
A HashMap
B HashSet
C TreeSet
D None of these
Correct Answer:  D. None of these
EXPLANATION

No collection allows duplicate keys. When a duplicate key is added to a Map, it overwrites the previous value. HashSet and TreeSet don't allow duplicates.

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