Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

hashmap

Total questions: 14

Worksheet time: 7mins

Name
Class
Date
1.

What is a HashMap?

a)

A data structure for storing ordered key-value pairs

b)

A data structure for storing unordered key-value pairs that uses a hash function for fast access.

c)

A data structure that uses trees to store values

d)

A data structure for storing homogeneous elements.

2.

How can you create an empty HashMap?

a)

hash_map = {}

b)

hash_map = []

c)

hash_map = set()

d)

hash_map = dict()

3.

How do you insert a key-value pair into a HashMap?

a)

hash_map.add("apple", 5)

b)

hash_map.put("apple", 5)

c)

hash_map["apple"] = 5

d)

hash_map.insert("apple", 5)

4.

How do you check if a key exists in a HashMap?

a)

key.exists("apple")

b)

hash_map.contains("apple")

c)

hash_map.has_key("apple")

d)

"apple" in hash_map

5.

What happens if you try to retrieve a value for a non-existing key in a HashMap?

a)

A KeyError will be raised

b)

It returns None

c)

It returns a default value if specified

d)

It returns an empty value (e.g., "" or 0).

6.

How do you remove a key-value pair from a HashMap?

a)

hash_map.remove("apple")

b)

hash_map.del("apple")

c)

- del hash_map["apple"]

d)

hash_map.delete("apple")

7.

How do you update the value for an existing key in a HashMap?

a)

hash_map["apple"] = new_value

b)

hash_map.update("apple", new_value)

c)

hash_map.set("apple", new_value)

d)

hash_map.insert("apple", new_value)

8.

How do you get all the keys in a HashMap?

a)

hash_map.all_keys()

b)

hash_map.keys()

c)

hash_map.get_keys()

d)

hash_map.get_all_keys()

9.

How does Python handle collisions in a HashMap (dictionary)?

a)

Python does not handle collisions, they result in an error.

b)

Python resolves collisions using methods like open addressing or chaining.

c)

Python always stores two keys at the same location if they collide

d)

Python simply ignores collisions and stores only the last added key

10.

How do you get all elements in a HashMap as pairs (key, value)?

a)

hash_map.pairs()

b)

hash_map.get_items()

c)

hash_map.items()

d)

hash_map.all()

11.

What is the average time complexity for the get operation in a HashMap

a)

O(n)

b)

O(log n)

c)

O(1)

d)

O(n^2)

12.

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?

a)

hash_map.get("apple")

b)

hash_map.get("apple", default_value)

c)

hash_map["apple"]

d)

hash_map.has_key("apple")

13.

How do you get all the values from a HashMap?

a)

hash_map.get_all_values()

b)

hash_map.all_values()

c)

hash_map.fetch_values(

d)

hash_map.values()

14.

What is the time complexity of iterating over all items in a Python dictionary (using dict.items() or similar)?

a)

O(1)

b)

O(log n)

c)

O(n)

d)

O(n^2)