Worksheetshashmap
Total questions: 14
Worksheet time: 7mins
What is a HashMap?
A data structure for storing ordered key-value pairs
A data structure for storing unordered key-value pairs that uses a hash function for fast access.
A data structure that uses trees to store values
A data structure for storing homogeneous elements.
How can you create an empty HashMap?
hash_map = {}
hash_map = []
hash_map = set()
hash_map = dict()
How do you insert a key-value pair into a HashMap?
hash_map.add("apple", 5)
hash_map.put("apple", 5)
hash_map["apple"] = 5
hash_map.insert("apple", 5)
How do you check if a key exists in a HashMap?
key.exists("apple")
hash_map.contains("apple")
hash_map.has_key("apple")
"apple" in hash_map
What happens if you try to retrieve a value for a non-existing key in a HashMap?
A KeyError will be raised
It returns None
It returns a default value if specified
It returns an empty value (e.g., "" or 0).
How do you remove a key-value pair from a HashMap?
hash_map.remove("apple")
hash_map.del("apple")
- del hash_map["apple"]
hash_map.delete("apple")
How do you update the value for an existing key in a HashMap?
hash_map["apple"] = new_value
hash_map.update("apple", new_value)
hash_map.set("apple", new_value)
hash_map.insert("apple", new_value)
How do you get all the keys in a HashMap?
hash_map.all_keys()
hash_map.keys()
hash_map.get_keys()
hash_map.get_all_keys()
How does Python handle collisions in a HashMap (dictionary)?
Python does not handle collisions, they result in an error.
Python resolves collisions using methods like open addressing or chaining.
Python always stores two keys at the same location if they collide
Python simply ignores collisions and stores only the last added key
How do you get all elements in a HashMap as pairs (key, value)?
hash_map.pairs()
hash_map.get_items()
hash_map.items()
hash_map.all()
What is the average time complexity for the get operation in a HashMap
O(n)
O(log n)
O(1)
O(n^2)
How do you check if a key "apple" exists in a HashMap and get its value without raising an error if the key does not exist?
hash_map.get("apple")
hash_map.get("apple", default_value)
hash_map["apple"]
hash_map.has_key("apple")
How do you get all the values from a HashMap?
hash_map.get_all_values()
hash_map.all_values()
hash_map.fetch_values(
hash_map.values()
What is the time complexity of iterating over all items in a Python dictionary (using dict.items() or similar)?
O(1)
O(log n)
O(n)
O(n^2)
