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.
Which feature best describes a hash map’s ability to store and access values?
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.
What happens when you try to add a duplicate value to a hash set?
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.
If you use a key that does not exist in a hash map to retrieve a value, what typically happens?
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.
What is the purpose of a hash function in hash maps and sets?
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.
What kind of data can usually be used as keys in a hash map?
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.
How efficient is searching for an element in a standard hash set?
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.
In hash maps, what is a collision?
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.
Which operation checks if a value exists in a hash set?
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.
Why are hash maps often preferred over lists for key-based lookups?
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.
What happens if you update the value for an existing key in a hash map?
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.
Does a standard hash set preserve the order of elements inserted?
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.
Given the list [2, 3, 2, 4, 3], how many unique elements are there when stored in a hash set?
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.
What occurs when you remove a key from a hash map?
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.
Can a hash map store a key with a null or empty value?
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.
Which scenario is best handled using a hash set?
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.
Given a hash map where 'cat' maps to 7 and 'dog' maps to 9, what is returned if you look up 'dog'?
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.