Hash Maps and Sets Essentials Quiz

Test your understanding of hash maps and sets with this beginner-friendly quiz covering key concepts, basic operations, use cases, and terminology for efficient data storage and retrieval.

  1. Basic Property of Hash Maps

    Which feature best describes a hash map’s ability to store and access values?

    1. It stores data in a sorted order
    2. It can only store numeric values
    3. It merges duplicate keys automatically
    4. It stores values associated with unique keys

    Explanation: Hash maps store values paired with unique keys, allowing quick lookups. They do not guarantee sorted order, so the first option is incorrect. Hash maps can store many data types, not just numerics, ruling out the third. Duplicate keys overwrite existing values, rather than merging, explaining why the fourth is incorrect.

  2. Hash Set Uniqueness

    What happens when you try to add a duplicate value to a hash set?

    1. The set removes all values
    2. The set allows multiple copies
    3. An error always occurs
    4. The duplicate is ignored

    Explanation: Hash sets only allow unique elements, so adding a duplicate value is ignored without changing the set. It does not remove all values or permit multiple copies; these are incorrect. Errors may not necessarily occur, making the fourth option less accurate.

  3. Retrieving Values

    If you use a key that does not exist in a hash map to retrieve a value, what typically happens?

    1. A new key is automatically added
    2. You get a default or null value
    3. The entire map is cleared
    4. A random value is returned

    Explanation: When a key is missing, hash maps typically return a default or null value. They do not clear the map, return random results, or auto-add new keys during retrieval—these options are all incorrect or describe uncommon behavior.

  4. Hash Function Role

    What is the purpose of a hash function in hash maps and sets?

    1. To sort the keys
    2. To encrypt values
    3. To map keys to storage locations
    4. To compress data

    Explanation: A hash function computes an index that determines where a key-value pair is stored. It neither sorts keys nor compresses data, as these distractors misrepresent its function. Hash functions are not primarily for encryption; their main role is efficient mapping.

  5. Key Data Types in Hash Maps

    What kind of data can usually be used as keys in a hash map?

    1. Immutable objects
    2. Any object including mutable ones
    3. Only floating-point numbers
    4. Only integers

    Explanation: Hash maps require keys that do not change after being added, making immutable objects ideal. Only allowing integers or floats is incorrect, as it would limit flexibility. Using mutable objects risks errors if their state changes, making this distractor incorrect.

  6. Searching in Hash Sets

    How efficient is searching for an element in a standard hash set?

    1. Takes exponential time
    2. Linear time in all cases
    3. Always slower than lists
    4. Constant time, on average

    Explanation: Hash sets typically provide average constant-time performance for searches due to their design. Lists often have slower, linear searches, making the second option wrong. Exponential or always linear time does not reflect hash set performance.

  7. Handling Key Collisions

    In hash maps, what is a collision?

    1. Two keys mapped to the same index
    2. A value copied twice
    3. All items deleted unexpectedly
    4. A key mapped to multiple values

    Explanation: A collision occurs when a hash function assigns two different keys to the same storage index. Keys mapping to multiple values is incorrect for hash maps. Duplicated values or unexpected deletion do not describe a collision.

  8. Membership Checking in Sets

    Which operation checks if a value exists in a hash set?

    1. Delete
    2. Sort
    3. Insert
    4. Contains

    Explanation: The 'contains' operation efficiently determines set membership. 'Insert' and 'delete' are used to add or remove elements, not to check presence. 'Sort' is generally unsupported for unordered hash sets.

  9. Benefits of Hash Maps

    Why are hash maps often preferred over lists for key-based lookups?

    1. They always use less memory
    2. They prevent duplicate values
    3. They support fast access using keys
    4. They return values in order

    Explanation: Hash maps provide quick access via unique keys, which is their main advantage over lists. Memory use and order are not guaranteed improvements over lists. Avoiding duplicate values is not a unique feature of hash maps.

  10. Mutating Values

    What happens if you update the value for an existing key in a hash map?

    1. A new key is created
    2. The map is cleared
    3. The old value is overwritten
    4. Both values are stored

    Explanation: Updating a key in a hash map replaces its old value with the new one. No new key is introduced, and both values are not stored simultaneously. Clearing the map does not occur as a result of this operation.

  11. Hash Sets and Order

    Does a standard hash set preserve the order of elements inserted?

    1. Only for numbers
    2. Yes, always
    3. Only in small sets
    4. No, order is not guaranteed

    Explanation: Standard hash sets do not guarantee that the insertion order is maintained. The options suggesting always, only for certain types, or for small sets are incorrect, as ordering is not a defined feature of typical hash sets.

  12. Counting Unique Elements

    Given the list [2, 3, 2, 4, 3], how many unique elements are there when stored in a hash set?

    1. 3
    2. 2
    3. 4
    4. 5

    Explanation: A hash set only stores unique values, so from the list [2, 3, 2, 4, 3], only 2, 3, and 4 remain—3 elements. The other amounts either miscount duplicates or overstate the total.

  13. Removing Elements from Hash Maps

    What occurs when you remove a key from a hash map?

    1. The key changes to a default value
    2. Its key-value pair is deleted
    3. The map becomes empty
    4. All values are set to zero

    Explanation: Removing a key from a hash map deletes its corresponding key-value pair. It does not empty the entire map or set all values to zero. The key cannot simply change to a default value; it is removed entirely.

  14. Null Values in Hash Maps

    Can a hash map store a key with a null or empty value?

    1. No, null values are converted
    2. Yes, but only for numbers
    3. Yes, if supported by the implementation
    4. No, it throws an error

    Explanation: Storing null or empty values with keys depends on the hash map's implementation. Some allow this, while others do not. Errors may only be thrown in unsupported cases, making the first option too absolute. Keys for numbers only is incorrect, and conversion is not always applicable.

  15. Practical Use Case

    Which scenario is best handled using a hash set?

    1. Checking for duplicate IDs
    2. Counting word frequency
    3. Merging two sorted lists
    4. Maintaining sorted scores

    Explanation: A hash set efficiently checks for uniqueness, such as detecting duplicate IDs. Counting word frequency is better with hash maps. Sorted lists involve order, making the other options unsuitable for hash sets.

  16. Hash Map Lookup Example

    Given a hash map where 'cat' maps to 7 and 'dog' maps to 9, what is returned if you look up 'dog'?

    1. 9
    2. cat
    3. dog
    4. 7

    Explanation: The key 'dog' maps to the value 9, so looking it up returns 9. The distractors 'cat' and 'dog' are incorrect, as they refer to keys or literals. The value 7 is linked to a different key.