AlgoMaster Logo

Dictionaries - Quiz

Last Updated: January 3, 2026

Dictionaries Exercises

31 quizzes

1
Code Completion

Access the price of the 'banana' item from the inventory dictionary and store it in total_cost

python
1
total_cost = inventory["banana"][]

Click an option to fill the blank:

2
Code Completion

Safely get a user's preferred_language from the settings dictionary, falling back to 'en' when the key is missing

python
1
preferred = settings.get("preferred_language", )

Click an option to fill the blank:

3
Code Completion

Create a dictionary that maps each product code to its length using a dictionary comprehension

python
1
code_lengths = {code: for code in product_codes}

Click an option to fill the blank:

4
Multiple Choice

Which statement correctly creates an empty dictionary in Python?

5
Multiple Choice

Given user = {'name': 'Sam', 'age': 25}, which expression safely returns 25?

6
Multiple Choice

Which method removes and returns an arbitrary key-value pair from a dictionary?

7
Multiple Choice

What does orders.keys() return for a dictionary orders?

8
Multiple Choice

Which dictionary comprehension creates {'a': 1, 'b': 2, 'c': 3} from letters = ['a','b','c']?

9
Multiple Choice

How do you access the published year of 'book2' in a nested dict library?

10
Multiple Choice

Which import is required to use defaultdict?

11
Multiple Choice

Which factory is best for grouping words by first letter with defaultdict?

12
Multiple Choice

Which OrderedDict method moves a key to the end or beginning?

13
Multiple Choice

Given c = Counter(['a','b','a']), what is c['c']?

14
Multiple Choice

Which Counter method returns the n most common elements and counts?

15
Sequencing

Order the steps to build a word frequency dictionary from a list of words using a standard dict.

Drag and drop to reorder, or use the arrows.

16
Sequencing

Order the steps to create a defaultdict that groups product IDs by category.

Drag and drop to reorder, or use the arrows.

17
Output Prediction

What is the output of this code using a basic dictionary and get()?

1config = {"timeout": 30, "retries": 3}
2value = config.get("delay", config["timeout"])
3print(value)
18
Output Prediction

Predict the output when using a dictionary comprehension to filter ages.

1ages = {"Ana": 19, "Bob": 17, "Cara": 21}
2adults = {name: age for name, age in ages.items() if age >= 18}
3print(len(adults))
19
Output Prediction

What does this nested dictionary access print?

1catalog = {
2    "books": {"count": 120},
3    "movies": {"count": 45}
4}
5item = "movies"
6print(catalog[item]["count"])
20
Output Prediction

What is printed when using defaultdict with int as the factory?

1from collections import defaultdict
2scores = defaultdict(int)
3scores["alice"] += 5
4print(scores["bob"])
21
Output Prediction

What is the output when using Counter on a list of tags?

1from collections import Counter
2tags = ['python', 'data', 'python', 'web']
3counts = Counter(tags)
4print(counts['python'])
22
Bug Spotting

Find the bug in this code that updates a nested dictionary of products by category.

Click on the line(s) that contain the bug.

python
1
def add_product(catalog, category, product_name):
2
    if category not in catalog:
3
        catalog[category] = []
4
    catalog[category][product_name] = True
5
    return catalog
23
Bug Spotting

Find the bug in this code that uses defaultdict to group user IDs by role.

Click on the line(s) that contain the bug.

python
1
from collections import defaultdict
2
 
3
def group_users_by_role(users):
4
    groups = defaultdict()
5
    for user in users:
6
        role = user['role']
7
        groups[role].append(user['id'])
8
    return groups
24
Matching

Match each dictionary-related concept to its best description.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

25
Matching

Match each Counter method or behavior with what it does.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

26
Fill in the Blanks

Complete the code to create a nested dictionary of users where each user has an email and an 'active' flag set to True.

python
1
def create_user_record(username, email):
2
profile =
3
profile[username] =
4
return profile

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the code to build a dictionary of squared values for only even numbers from a list.

python
1
def even_squares(numbers):
2
return {value: for value in numbers if }

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the code to initialize a defaultdict used for aggregating log messages by level.

python
1
from collections import defaultdict
2
3
def build_log_index(entries):
4
logs_by_level = defaultdict()
5
for level, message in entries:
6
logs_by_level[level].(message)
7
return logs_by_level

Click an option to fill blank 1:

29
Fill in the Blanks

Complete the code to create an OrderedDict sorted by keys from a regular dictionary.

python
1
from collections import OrderedDict
2
3
def sort_config(config):
4
sorted_items = sorted(config.())
5
return OrderedDict()

Click an option to fill blank 1:

30
Hotspot Selection

Click the line that will raise a KeyError when accessing a nested dictionary of profiles.

Click on the line to select.

python
1
profiles = {
2
    "alice": {"email": "a@example.com"},
3
    "bob": {"email": "b@example.com"}
4
}
5
print(profiles["carol"]["email"])
31
Hotspot Selection

Click the line that incorrectly reorders an OrderedDict of tasks.

Click on the line to select.

python
1
from collections import OrderedDict
2
 
3
tasks = OrderedDict()
4
tasks['low'] = []
5
tasks['medium'] = []
6
tasks['high'] = []
7
 
8
tasks.move_to_end('medium', last=False)
9
print(list(tasks.keys()))