Java Collections Quiz - MCQ - Multiple Choice Questions

 Java collections are fundamental in Java programming, providing a wide range of data structures and algorithms to efficiently store and manipulate data. 

This blog post presents a quiz to test your understanding of Java collections. The quiz consists of 25 useful Java collections framework multiple-choice questions to self-test your knowledge of Java collections framework classes and interfaces.

Java everything about Java 8 features: Java 8 Tutorial and Examples

Check out 100+ quiz questions: 100+ Quiz Questions to Test Your Java, Spring Boot, Microservices, Hibernate, REST API Skills

The answer and explanation have been given for each MCQ.

1. Which of the following is a part of the Java Collections Framework?

a) ArrayList
b) Array
c) int[]
d) String

Answer:

a) ArrayList

Explanation:

ArrayList is a part of the Java Collections Framework, whereas Array and int[] are native array structures, and String is a final class representing strings.

2. Which is the root interface of the Java Collection framework hierarchy?

a) Collection
b) Root
c) Collections
d) List/Set

Answer:

a) Collection

Explanation:

In the Java Collections framework, the Collection interface is the root interface of the hierarchy. It provides the foundation upon which the List, Set, and Queue interfaces are built. On the other hand, Collections is a utility class that provides static methods to operate on or return collections. The options List/Set and Root are not the root interfaces of the Java Collection framework hierarchy.

3. Which collection does not allow duplicate elements?

a) List
b) Set
c) Map
d) All of the above

Answer:

b) Set

Explanation:

The Set interface does not allow duplicate elements. The List allows duplicates, and Map deals with key-value pairs where keys cannot be duplicate.

4. Which collection allows you to retrieve elements in the order they were inserted?

a) HashSet
b) TreeMap
c) LinkedHashMap
d) PriorityQueue

Answer:

c) LinkedHashMap

Explanation:

LinkedHashMap maintains the insertion order.

5. What is the implementation of the List interface?

a. HashMap
b. HashSet
c. LinkedList
d. LinkedHashSet

Answer:

Answer

Explanation:

c. LinkedList

6. What are the implementation classes of the List interface?

a. ArrayList
b. HashSet
c. LinkedList
d. Vector

Answer:

a, c, d

Explanation:

ArrayList, LinkedList, and Vector are implementation classes of the List interface.

7. Which class is a resizable array implementation?

a) LinkedList
b) ArrayList
c) HashSet
d) ArrayDeque

Answer:

b) ArrayList

Explanation:

ArrayList is essentially a dynamic array that can grow as needed.

8. Which collection ensures that elements are processed in first-in-first-out (FIFO) order?

a) HashSet
b) LinkedHashSet
c) LinkedList
d) PriorityQueue

Answer:

c) LinkedList

Explanation:

LinkedList, when used as a queue, ensures FIFO ordering.

9. Which of the following collections is sorted by its natural ordering?

a) HashMap
b) TreeMap
c) LinkedHashMap
d) Hashtable

Answer:

b) TreeMap

Explanation:

TreeMap stores its elements in a red-black tree structure, sorting them based on their natural ordering or by a comparator provided during creation.

10. Which collection class is synchronized?

a) HashSet
b) TreeSet
c) Hashtable
d) HashMap

Answer:

c) Hashtable

Explanation:

Hashtable is synchronized, meaning it's thread-safe. Other collections like HashMap are not synchronized by default.

11. Which interface provides methods to insert, remove, and inspect elements?

a) List
b) Set
c) Queue
d) Map

Answer:

c) Queue

Explanation:

The Queue interface provides methods like offer, poll, and peek for such operations.

12. Which of these is not a method in the Collection interface?

a) add()
b) get()
c) remove()
d) size()

Answer:

b) get()

Explanation:

The get() method is from the List interface, not the general Collection interface.

13. Which of the following collections is thread-safe?

a) Vector
b) ArrayList
c) LinkedList
d) TreeSet

Answer:

a) Vector

Explanation:

Vector is synchronized, meaning it's thread-safe. The other collections listed are not synchronized by default.

14. Which method removes all elements from a collection?

a) clear()
b) deleteAll()
c) removeAll()
d) purge()

Answer:

a) clear()

Explanation:

The clear() method is used to remove all elements from a collection.

15. What are the implementation classes of the Set interface?

a. HashSet
b. LinkedHashSet
c. ArrayList
d. TreeSet

Answer:

a,b,d

Explanation:

HashSet, LinkedHashSet, and TreeSet implementation classes of the Set interface.

16. In which collection are elements stored as key-value pairs?

a) List
b) Set
c) Map
d) Queue

Answer:

c) Map

Explanation:

The Map interface stores elements as key-value pairs.

17. Which of the following lists can contain the same element multiple times?

a) Set
b) Map
c) List
d) None of the above

Answer:

c) List

Explanation:

A List can contain duplicate elements.

18. Which method is used to check if a collection contains a certain element?

a) hasElement()
b) includes()
c) check()
d) contains()

Answer:

d) contains()

Explanation:

The contains() method is used to check if an element is present in a collection.

19. Which class provides a thread-safe implementation of the List?

a) ArrayList
b) CopyOnWriteArrayList
c) LinkedList
d) Vector

Answer:

b) CopyOnWriteArrayList

Explanation:

CopyOnWriteArrayList is a thread-safe variant of ArrayList in which all modifications are implemented by creating a separate copy of the underlying array.

20. Which collection provides a guaranteed constant time for basic operations like add, remove, and contains?

a) TreeSet
b) LinkedList
c) HashSet
d) TreeMap

Answer:

c) HashSet

Explanation:

HashSet offers constant-time performance for the basic operations assuming the hash function disperses the elements properly among the buckets.

21. Which method retrieves, but does not remove, the head (first element) of a Queue?

a) get()
b) pop()
c) peek()
d) fetch()

Answer:

c) peek()

Explanation:

The peek() method retrieves the element at the head of the queue without removing it.

22. Which of the following cannot contain a null value?

a) TreeMap
b) HashMap
c) LinkedHashMap
d) Hashtable

Answer:

a) TreeMap

Explanation:

TreeMap does not allow null keys because of ordering. The others can have at least one null key and multiple null values.

23. Which collection is synchronized and does not allow null values?

a) HashMap
b) Hashtable
c) LinkedHashMap
d) TreeMap

Answer:

b) Hashtable

Explanation:

Hashtable is synchronized and does not allow null keys or values.

24. Which interface represents a double-ended queue?

a) List
b) Set
c) Deque
d) Map

Answer:

c) Deque

Explanation:

The Deque interface represents a double-ended queue where you can insert and remove elements from both ends.

25. Which method is used to obtain the size of a collection?

a) length()
b) getLength()
c) count()
d) size()

Answer:

d) size()

Explanation:

The size() method returns the number of elements in a collection.

26. Which of the following is ordered and sorted by its natural ordering or by a comparator?

a) HashSet
b) LinkedHashSet
c) TreeSet
d) List

Answer:

c) TreeSet

Explanation:

TreeSet is a sorted set and orders its elements based on their natural ordering or by a comparator provided at set creation time.

27. Which list method replaces the element at a specified position?

a) replace()
b) set()
c) put()
d) insert()

Answer:

b) set()

Explanation:

The set() method replaces the element at the specified position in the list.

28. Which of these is not a direct implementation of the Map interface?

a) HashMap
b) TreeMap
c) HashSet
d) LinkedHashMap

Answer:

c) HashSet

Explanation:

HashSet is an implementation of the Set interface, not Map.

29. What are the implementation classes of the Map interface?

a. HashMap
b. LinkedHashMap
c. TreeMap
d. All of above

Answer:

d. All of the above

Explanation:

HashMap, LinkedHashMap, and TreeMap are implementation classes of the Map interface.

30. HashSet internally uses?

a.Set
b.HashMap
c.List
d.Collection

Answer:

b.HashMap

Explanation:

Internally, HashSet uses HashMap to store its elements. The elements you add to the HashSet become the keys in the underlying HashMap, with a constant dummy value associated with every key. This allows HashSet to leverage the fast key lookup capabilities of HashMap to provide quick implementations of its own add, remove, and contains methods.

31. Which data structure ArrayList internally uses?

a. LinkedList
b. Array
c. Doubly LinkedList
d. None

Answer:

b) Array

Explanation:

ArrayList internally uses a dynamic array to store its elements. As the list grows, and the capacity of the underlying array is exceeded, ArrayList creates a new, larger array and copies the old elements to it. This provides efficient random access but can make the insertion and deletion of elements in the middle of the list more costly compared to a LinkedList.

32. Which of these classes should be preferred to be used as a key in a HashMap?

a.String
b.Integer
c.Double
d.Any of these

Answer:

d. Any of these

Explanation:

All of the classes listed (String, Integer, and Double) override the hashCode() and equals() methods, making them suitable to be used as keys in a HashMap. In fact, any class that correctly overrides these methods can be used as a key. However, it's essential that the key remains immutable while it's in the map to ensure the map's integrity. Among the options given, String, Integer, and Double are all immutable, so they are good choices for keys in a HashMap.

Comments