Java Collections Essentials Quiz Quiz

Test your basic understanding of Java Collections framework concepts, behaviors, and internal mechanisms with these easy multiple-choice questions.

  1. List vs Set Distinction

    Which of the following statements correctly describes the difference between a List and a Set in the Java Collections framework?

    1. A List allows duplicate elements and maintains insertion order.
    2. A Set allows duplicate elements and does not maintain order.
    3. A Set always sorts its elements by their values.
    4. A List always stores elements in a random order.
    5. A List stores key-value pairs and forbids duplicates.
  2. Preserving Insertion Order

    Which Java collection guarantees that elements are iterated in the same order as they were inserted?

    1. LinkedHashSet
    2. HashSet
    3. VectorSet
    4. SortList
    5. TreeSet
  3. Hash Code and Equals Contract

    According to the Java Collections framework, what must be true if obj1.equals(obj2) returns true?

    1. obj1 and obj2 must be of different classes
    2. obj1.hashCode() and obj2.hashCode() must always differ
    3. obj1.hashCode() must equal obj2.hashCode()
    4. obj1 and obj2 must have been created in the same method
    5. obj1.toString() must equal obj2.toString()
  4. HashMap Internal Structure

    Which data structure is primarily used to store key-value pairs in a HashMap before Java 8 when hash collisions occur?

    1. ArrayList
    2. Queue
    3. LinkedList
    4. PriorityQueue
    5. Red-Black Tree
  5. 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?

    1. The map resets to empty
    2. An exception is always thrown
    3. Only the new updated value
    4. Either the previous value or the updated value, depending on timing
    5. Always a corrupted value
  6. 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?

    1. Doubly Linked List
    2. MinHeap
    3. Binary Heap
    4. Stack
    5. Red-Black Tree
  7. Resizing a HashMap

    What triggers a HashMap to increase its internal capacity during runtime?

    1. During every put operation
    2. When the hash code of an existing element changes
    3. Whenever a duplicate key is added
    4. When the load factor reaches or exceeds a certain threshold (commonly 0.75)
    5. When elements are removed
  8. 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?

    1. The set sorts the elements before adding the new one
    2. Both elements are always stored without checking
    3. The set resizes immediately
    4. The set uses equals() to check if the elements are duplicates before adding
    5. A compilation error occurs
  9. Thread Safety in HashMap

    Which approach can make a HashMap thread-safe for use by multiple threads?

    1. Removing the load factor
    2. Sorting the map after every insertion
    3. Disabling garbage collection during operations
    4. Converting the map to a List
    5. Synchronizing all access to the map
  10. HashTable vs HashMap

    Which of the following is a key difference between HashTable and HashMap in Java?

    1. HashTable does not use hashing
    2. HashTable allows duplicate keys
    3. HashMap requires keys to be in sorted order
    4. HashTable is synchronized by default while HashMap is not
    5. HashMap does not store key-value pairs