Java Collections Framework MCQ Questions and Answers

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

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

Answer:

d) HashTable

Explanation:

HashTable is part of the Java Collections API but not a part of the Collections Framework introduced in Java 2. It is considered a legacy class.

2. What does the 'Set' interface primarily ensure?

a) Duplicate elements
b) Ordered collection of elements
c) Elements are indexed
d) No duplicate elements

Answer:

d) No duplicate elements

Explanation:

The Set interface models the mathematical set abstraction and ensures that no duplicate elements are stored.

3. Which class is used for resizable-array implementation of the List interface?

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

Answer:

c) ArrayList

Explanation:

ArrayList is used as it provides a resizable-array, which means that the size of the list can be increased or decreased dynamically.

4. Which of the following is true about the PriorityQueue class?

a) It does not guarantee to sort the elements
b) It orders elements in a FIFO (First-In-First-Out) manner
c) It is used to process elements based on priority
d) It allows duplicate elements

Answer:

c) It is used to process elements based on priority

Explanation:

PriorityQueue is a queue implementation that orders elements based on their natural ordering or by a Comparator provided at queue construction time.

5. What is the primary difference between a HashSet and a LinkedHashSet?

a) LinkedHashSet is not a part of the Java Collections Framework
b) LinkedHashSet maintains insertion order
c) HashSet allows duplicate elements
d) LinkedHashSet is slower than HashSet

Answer:

b) LinkedHashSet maintains insertion order

Explanation:

LinkedHashSet extends HashSet, but it also maintains a doubly-linked list across all elements, ensuring that the order of insertion is maintained.

6. What interface represents a last-in-first-out (LIFO) stack of objects?

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

Answer:

d) Deque

Explanation:

Deque interface extends Queue and allows insertion and removal of elements at both ends, thus functioning as a LIFO stack.

7. What does the 'Map' interface in Java not allow?

a) Duplicate keys
b) Null values
c) Duplicate values
d) Iterating

Answer:

a) Duplicate keys

Explanation:

A Map cannot contain duplicate keys; each key can map to at most one value.

8. In a TreeMap, which of the following is true about the ordering of the elements?

a) The elements are ordered by hashcode
b) The elements are stored in insertion order
c) The elements are ordered in descending order
d) The elements are ordered using their natural ordering or by a comparator

Answer:

d) The elements are ordered using their natural ordering or by a comparator

Explanation:

TreeMap stores its elements in a red-black tree, which orders them based on their natural ordering or by a comparator provided at map creation time.

9. Which of these methods deletes all the elements from a collection?

a) clear()
b) delete()
c) removeAll()
d) reset()

Answer:

a) clear()

Explanation:

The clear() method is used to delete all the elements of a collection, resetting its size to zero.

10. Which interface forms the root of the collection hierarchy in Java?

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

Answer:

d) Collection

Explanation:

The Collection interface is the root of the collection hierarchy. List, Set, and Queue interfaces extend Collection.

11. What will Collections.emptySet() return?

a) A new set instance with no elements
b) A null pointer
c) An UnsupportedOperationException
d) A singleton set with a default value

Answer:

a) A new set instance with no elements

Explanation:

Collections.emptySet() returns a special immutable empty set. No elements can be added to this set.

12. Which class provides a resizable array and implements the List interface?

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

Answer:

b) ArrayList

Explanation:

ArrayList provides a resizable-array, which can be expanded or contracted dynamically.

13. Which of the following is true about the hashCode() method?

a) It returns a unique identifier for each object
b) It is used in the indexing of collections
c) It can never return the same value for two different objects
d) It is not relevant to collection classes

Answer:

b) It is used in the indexing of collections

Explanation:

hashCode() is used by hash-based collections like HashSet, HashMap, etc., for efficiently locating objects.

14. Which of these is a FIFO (first-in-first-out) data structure?

a) Stack
b) PriorityQueue
c) LinkedList
d) TreeMap

Answer:

c) LinkedList

Explanation:

LinkedList can be used as a queue which follows FIFO order.

15. What is the initial capacity of a Vector class in Java Collections Framework?

a) 10
b) 16
c) 5
d) 0

Answer:

a) 10

Explanation:

The default initial capacity of a Vector is 10. If no capacity is specified, it uses this default value.

16. Which of these is synchronized?

a) ArrayList
b) HashMap
c) Vector
d) HashSet

Answer:

c) Vector

Explanation:

Vector is synchronized, meaning it is thread-safe. The others are not synchronized.

17. Which method is used to retrieve but not remove the head of a Queue?

a) poll()
b) peek()
c) remove()
d) extract()

Answer:

b) peek()

Explanation:

peek() retrieves the head of the queue without removing it. If the queue is empty, it returns null.

18. Which of the following collections classes allows the storage of many null values?

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

Answer:

c) HashSet

Explanation:

HashSet allows the storage of many null values. TreeSet and TreeMap do not allow null keys (and in the case of TreeSet, not even null values).

19. Which collection class is typically used to represent a 'LIFO' stack?

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

Answer:

c) ArrayDeque

Explanation:

ArrayDeque can be used as a stack and is more efficient than Stack class.

20. In a HashMap, when a key-value pair is stored, the key object is used as a ________.

a) value
b) reference
c) index
d) hashcode

Answer:

d) hashcode

Explanation:

In HashMap, the key object's hashcode is used to find the bucket where the Entry object (which contains the key-value pair) will be stored.

21. The Collections Framework was introduced in which version of Java?

a) Java 1.2
b) Java 1.0
c) Java 1.4
d) Java 1.5

Answer:

a) Java 1.2

Explanation:

The Collections Framework was a major addition in JDK 1.2.

22. What will be the result of invoking remove(0) on an empty LinkedList object?

a) NullPointerException
b) IndexOutOfBoundsException
c) UnsupportedOperationException
d) The method will simply return without any exception

Answer:

b) IndexOutOfBoundsException

Explanation:

Attempting to remove an element from an empty list at any index will result in IndexOutOfBoundsException.

23. Which of these interfaces extends the Collection interface?

a) Map
b) List
c) Both Map and List
d) Neither Map nor List

Answer:

b) List

Explanation:

The List interface extends Collection. Map is not a true Collection, and it does not extend Collection interface.

24. Which class implements a bit vector that allows users to set or clear individual bits?

a) BitSet
b) EnumSet
c) BitSetMap
d) Vector

Answer:

a) BitSet

Explanation:

BitSet creates a special type of array that holds bit values and can increase in size as needed.

25. Which method in the Collections class is synchronized?

a) sort()
b) min()
c) synchronizedCollection()
d) reverse()

Answer:

c) synchronizedCollection()

Explanation:

synchronizedCollection() is used to return a synchronized (thread-safe) collection backed by the specified collection.

26. What is the behavior of the add() method in a Set collection?

a) It adds the element if it is not already present in the set
b) It adds the element and returns true
c) It replaces the existing element if it is already present
d) It throws an exception if the element is already present

Answer:

a) It adds the element if it is not already present in the set

Explanation:

In a Set, the add() method will add the element only if it is not already present in the set, ensuring no duplicates.

27. What will happen if you try to sort a list that contains null elements?

a) NullPointerException
b) The list will be sorted and null will be treated as the lowest value
c) The sort operation will ignore null values
d) A runtime exception other than NullPointerException

Answer:

a) NullPointerException

Explanation:

Attempting to sort a list that contains null elements will result in a NullPointerException, as null cannot be compared with non-null elements.

28. Which of these is not a part of the Collections Framework?

a) Iterator
b) Enumeration
c) Series
d) ListIterator

Answer:

c) Series

Explanation:

Series is not a part of the Java Collections Framework. Iterator, Enumeration, and ListIterator are all interfaces provided in the framework.

29. What does the Iterator interface provide?

a) A way to efficiently process elements in parallel
b) The ability to navigate forward and backward through a collection
c) The ability to add elements to a collection
d) A way to iterate over the elements of a collection

Answer:

d) A way to iterate over the elements of a collection

Explanation:

The Iterator interface provides methods to iterate over the elements of a collection one at a time.

30. Which interface is used to represent a sequence where duplicates are allowed?

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

Answer:

b) List

Explanation:

The List interface allows a sequence of elements where duplicates are allowed.

31. What distinguishes a HashMap from a Hashtable in Java Collections?

a) HashMap is synchronized, while Hashtable is not
b) Hashtable does not allow null keys or values, while HashMap does
c) HashMap maintains insertion order, while Hashtable does not
d) Hashtable is faster than HashMap

Answer:

b) Hashtable does not allow null keys or values, while HashMap does

Explanation:

Hashtable is a legacy class and is synchronized; it does not allow any null key or null value. HashMap, part of the Java Collections Framework, allows one null key and multiple null values.

32. Which method in the List interface inserts the specified element at the specified position?

a) add(int index, E element)
b) set(int index, E element)
c) insert(int index, E element)
d) push(int index, E element)

Answer:

a) add(int index, E element)

Explanation:

The add(int index, E element) method inserts the element at the specified position in the list.

33. How does a ConcurrentHashMap differ from a HashMap?

a) ConcurrentHashMap does not allow null keys or values
b) ConcurrentHashMap allows one null key
c) ConcurrentHashMap is not thread-safe
d) All of the above

Answer:

a) ConcurrentHashMap does not allow null keys or values

Explanation:

ConcurrentHashMap class does not allow null keys or values.

34. What is the default load factor of a HashMap in Java?

a) 0.75
b) 1.0
c) 0.5
d) 0.25

Answer:

a) 0.75

Explanation:

The default load factor for a HashMap is 0.75, which offers a good trade-off between time and space costs.

35. Which of the following methods is defined in the Collection interface?

a) keySet()
b) values()
c) entrySet()
d) add(E e)

Answer:

d) add(E e)

Explanation:

add(E e) is a method defined in the Collection interface, used to add an element to the collection.

36. What will happen if two different HashMap keys have the same hashcode?

a) The second key will replace the first
b) HashMap will store both keys in the same bucket with different entries
c) A runtime exception will be thrown
d) The second key will be rejected

Answer:

b) HashMap will store both keys in the same bucket with different entries

Explanation:

If two keys have the same hashcode, they will be stored in the same bucket but as separate entries (this is called a collision).

37. Which of these is not a feature of the Java Collections Framework?

a) Reduces programming effort
b) Increases the performance of data structures
c) Restricts the usage of generics
d) Provides high-quality data structures

Answer:

c) Restricts the usage of generics

Explanation:

The Java Collections Framework fully supports generics, which enhances type safety and reduces the need for type casting.

38. What is the primary difference between Comparable and Comparator interfaces?

a) Comparable is used for sorting and Comparator is not
b) Comparator can be written for any class, but Comparable affects the original class
c) Comparator is used for collections only
d) Comparable is faster than Comparator

Answer:

b) Comparator can be written for any class, but Comparable affects the original class

Explanation:

Comparable is implemented by a class to compare its instances, while Comparator is a separate class used to compare instances of different classes.

39. Which collection class provides constant-time performance for basic operations?

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

Answer:

c) HashSet

Explanation:

HashSet offers constant time performance for basic operations like add, remove, contains, and size, assuming the hash function disperses elements properly.

40. What is the advantage of using a LinkedHashMap over a HashMap?

a) Faster access and insertion
b) Maintains insertion order
c) Allows one null key and multiple null values
d) Better for threading

Answer:

b) Maintains insertion order

Explanation:

LinkedHashMap maintains a doubly-linked list running through all its entries, thus maintaining the order of insertion.

41. What exception is thrown when an object is not found in a collection?

a) ObjectNotFoundException
b) NoSuchElementException
c) ElementNotFoundException
d) CollectionException

Answer:

b) NoSuchElementException

Explanation:

NoSuchElementException is thrown by methods like nextElement() of Enumeration when no more elements exist.

42. Which of these methods is not part of the Map interface?

a) clear()
b) remove(Object key)
c) get(Object key)
d) add(Object key, Object value)

Answer:

d) add(Object key, Object value)

Explanation:

The Map interface does not have an add() method; it uses put(Object key, Object value) to insert elements.

43. What does Collections.synchronizedList(list) return?

a) A synchronized list backed by the specified list
b) A copy of the original list
c) A new list with the same elements in reverse order
d) A list where all operations are performed using the wait-notify mechanism

Answer:

a) A synchronized list backed by the specified list

Explanation:

Collections.synchronizedList(list) returns a synchronized (thread-safe) list backed by the specified list.

44. What is the primary purpose of the WeakHashMap class?

a) To store weak references to its keys
b) To improve performance using weak hash codes
c) To allow garbage collection of keys when there are no longer any references to them
d) To use less memory compared to HashMap

Answer:

c) To allow garbage collection of keys when there are no longer any references to them

Explanation:

WeakHashMap is a hashtable-based implementation that stores only weak references to its keys, allowing keys to be garbage-collected if they are no longer referenced elsewhere.

45. Which interface represents a queue that supports operations at both ends?

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

Answer:

c) Deque

Explanation:

Deque (double-ended queue) allows us to add or remove elements from both ends.

46. How does a CopyOnWriteArrayList differ from a regular ArrayList?

a) It is not resizable
b) Every modification creates a fresh copy of the underlying array
c) It allows concurrent modification without throwing ConcurrentModificationException
d) Both b and c

Answer:

d) Both b and c

Explanation:

CopyOnWriteArrayList creates a new array upon modification, thus allowing concurrent iterations without throwing ConcurrentModificationException.

47. What will Collections.unmodifiableList(list) return?

a) A modifiable copy of the original list
b) An unmodifiable view of the specified list
c) A list that allows only addition of new elements
d) A list that automatically sorts itself

Answer:

b) An unmodifiable view of the specified list

Explanation:

Collections.unmodifiableList(list) returns an unmodifiable view of the specified list, which means no changes can be made to the List through this view.

48. Which of the following best describes the properties of a TreeMap?

a) It does not allow null keys and orders the keys in natural order
b) It allows one null key and does not order the keys
c) It is not synchronized and is slow
d) It allows multiple null keys

Answer:

a) It does not allow null keys and orders the keys in natural order

Explanation:

TreeMap does not allow null keys and maintains the keys in sorted order (natural order or using a comparator).

49. Which method would you use to obtain an array from a Collection?

a) toArray()
b) toList()
c) toSet()
d) toMap()

Answer:

a) toArray()

Explanation:

The toArray() method is used to obtain an array containing all of the elements in the collection.

50. What is the behavior of the retainAll() method in the Collection interface?

a) It removes all the elements from the collection
b) It retains only those elements that are also in the specified collection
c) It adds a collection of elements to the existing collection
d) It compares two collections and returns true if they have the same elements

Answer:

b) It retains only those elements that are also in the specified collection

Explanation:

The retainAll() method is used to retain only the elements in the collection that are contained in the specified collection.

Comments