21.) What is the primary purpose of the java.util.Collections class in Java?
A) To create new collections
B) To perform various utility operations on collections
C) To provide a base class for all collections
D) To define standard collection interfaces
22.) In a HashSet, how are elements accessed?
A) By an index
B) By a key
C) By a hash code
D) By order of insertion
23.) What is the key characteristic of a HashSet regarding element ordering?
A) Maintains elements in natural order
B) Maintains elements in the order of insertion
C) Sorts elements in ascending order
D) No specific order is guaranteed
24.) In the context of Java collections, what does “fail-fast” behavior refer to?
A) Collections that are prone to memory leaks
B) Collections that are inefficient for large data sets
C) Collections that throw exceptions if modified while being iterated
D) Collections that never throw exceptions
25.) Which interface provides a way to traverse elements in a collection in a forward direction only?
A) Iterable
B) Collection
C) ListIterator
D) Iterator
26.) What is the primary advantage of using an Iterator over a traditional for loop when iterating through a collection?
A) Iterator provides better performance.
B) Iterator automatically handles concurrent modification.
C) Iterator can iterate in reverse order.
D) Iterator can be used with primitive data types.
27.) Which collection does not support the foreach loop (enhanced for loop) for iteration?
A) ArrayList
B) LinkedList
C) HashSet
D) HashMap
28.) What is the alternative way to iterate over the elements of a HashMap using the foreach loop?
A) Convert the values to an ArrayList and iterate over it.
B) Use the “keySet()” method to get keys and then retrieve values.
C) Use the “entrySet()” method to access key-value pairs.
D) Iterate over the keys and retrieve values using the “get()” method.
29.) In which interface is the foreach loop (enhanced for loop) implemented for collections?
A) Iterable
B) Collection
C) Iterator
D) List
30.) Which method of the Iterator interface is used to retrieve the next element in a collection?
A) hasNext()
B) next()
C) previous()
D) get()