Test your basic understanding of Java Collections framework concepts, behaviors, and internal mechanisms with these easy multiple-choice questions.
List vs Set Distinction
Which of the following statements correctly describes the difference between a List and a Set in the Java Collections framework?
- A List allows duplicate elements and maintains insertion order.
- A Set allows duplicate elements and does not maintain order.
- A Set always sorts its elements by their values.
- A List always stores elements in a random order.
- A List stores key-value pairs and forbids duplicates.
Preserving Insertion Order
Which Java collection guarantees that elements are iterated in the same order as they were inserted?
- LinkedHashSet
- HashSet
- VectorSet
- SortList
- TreeSet
Hash Code and Equals Contract
According to the Java Collections framework, what must be true if obj1.equals(obj2) returns true?
- obj1 and obj2 must be of different classes
- obj1.hashCode() and obj2.hashCode() must always differ
- obj1.hashCode() must equal obj2.hashCode()
- obj1 and obj2 must have been created in the same method
- obj1.toString() must equal obj2.toString()
HashMap Internal Structure
Which data structure is primarily used to store key-value pairs in a HashMap before Java 8 when hash collisions occur?
- ArrayList
- Queue
- LinkedList
- PriorityQueue
- Red-Black Tree
Concurrent Collection Behavior
When one thread is writing to a concurrent map and another reads at the same time, what value might the reading thread see?
- The map resets to empty
- An exception is always thrown
- Only the new updated value
- Either the previous value or the updated value, depending on timing
- Always a corrupted value
Handling Hash Collisions in Java 8+
Starting with Java 8, which data structure is used by HashMap to handle buckets with many colliding keys, improving lookup times?
- Doubly Linked List
- MinHeap
- Binary Heap
- Stack
- Red-Black Tree
Resizing a HashMap
What triggers a HashMap to increase its internal capacity during runtime?
- During every put operation
- When the hash code of an existing element changes
- Whenever a duplicate key is added
- When the load factor reaches or exceeds a certain threshold (commonly 0.75)
- When elements are removed
HashSet Duplicate Handling
If you try to add an element to a HashSet and its hash code matches that of an existing element, what happens next?
- The set sorts the elements before adding the new one
- Both elements are always stored without checking
- The set resizes immediately
- The set uses equals() to check if the elements are duplicates before adding
- A compilation error occurs
Thread Safety in HashMap
Which approach can make a HashMap thread-safe for use by multiple threads?
- Removing the load factor
- Sorting the map after every insertion
- Disabling garbage collection during operations
- Converting the map to a List
- Synchronizing all access to the map
HashTable vs HashMap
Which of the following is a key difference between HashTable and HashMap in Java?
- HashTable does not use hashing
- HashTable allows duplicate keys
- HashMap requires keys to be in sorted order
- HashTable is synchronized by default while HashMap is not
- HashMap does not store key-value pairs