Java Collections Framework MCQs

11.) What is the time complexity for retrieving a value by key in a HashMap?

A) O(1)
B) O(log n)
C) O(n)
D) O(n log n)

Answer: Option A

Explanation: Retrieving a value by key in a HashMap is typically a constant-time operation.

12.) Which interface provides the basic functionality for all collections in the Java Collections Framework?

A) Collection
B) List
C) Set
D) Map

Answer: Option A

Explanation: The Collection interface provides the basic functionality common to all collections.

13.) What is the main purpose of the Iterator interface in Java?

A) To add elements to a collection
B) To iterate over the elements in a collection
C) To sort the elements in a collection
D) To retrieve the first element in a collection

Answer: Option B

Explanation: The Iterator interface is used to iterate over the elements in a collection.

14.) Which collection allows elements to be stored in a key-value pair format?

A) ArrayList
B) LinkedList
C) HashSet
D) HashMap

Answer: Option D

Explanation: HashMap allows elements to be stored in key-value pairs.

15.) Which collection allows elements to be stored in an ordered sequence, and elements can be accessed by their index?

A) List
B) Set
C) Map
D) Queue

Answer: Option A

Explanation: The List interface allows elements to be stored in an ordered sequence and accessed by their index.

16.) Which collection allows elements to be stored in a sorted order?

A) ArrayList
B) LinkedList
C) TreeSet
D) HashMap

Answer: Option C

Explanation: TreeSet is a collection that stores elements in sorted order.

17.) What is the purpose of the Comparator interface in Java?

A) To compare elements for equality
B) To define a natural ordering for elements
C) To provide a custom ordering for elements
D) To create hash codes for elements

Answer: Option C

Explanation: The Comparator interface is used to provide custom ordering for elements in collections.

18.) Which Java collection is typically used to implement a queue data structure?

A) ArrayList
B) LinkedList
C) HashSet
D) HashMap

Answer: Option B

Explanation: LinkedList is commonly used to implement a queue data structure.

19.) Which Java collection is typically used to implement a priority queue data structure?

A) ArrayList
B) LinkedList
C) PriorityQueue
D) TreeMap

Answer: Option C

Explanation: PriorityQueue is commonly used to implement a priority queue data structure.

20.) Which Java collection allows elements to be retrieved in a last-in-first-out (LIFO) order?

A) ArrayList
B) LinkedList
C) HashSet
D) Stack

Answer: Option D

Explanation: The Stack class in Java allows elements to be retrieved in a LIFO order.

Leave a Reply

Your email address will not be published. Required fields are marked *