wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Chapter 9: Dictionaries

Total questions: 30

Worksheet time: 23mins

Name
Class
Date
1.

What is the main difference between a list and a dictionary in Python?

a)

A. Lists store data in pairs of key and value

b)

B. Dictionaries store data in pairs of key and value

c)

C. Lists can only store numbers

d)

D. Dictionaries can only store strings

2.

How do you create an empty dictionary?

a)

A. dict = []

b)

B. dict = ()

c)

C. dict = {}

d)

D. dict = ''

3.

What will this code display?
student = {'name': 'Ali', 'age': 20}
print(student['name'])

a)

name

b)

Ali

c)

age

d)

student

4.

Which symbol is used to define a dictionary literal?

a)

Square brackets [ ]

b)

Curly braces { }

c)

Parentheses ( )

d)

Angle brackets < >

5.

What will happen if you try to access a key that doesn’t exist in a dictionary?

a)

Returns 0

b)

Returns None

c)

Raises a KeyError

d)

Skips the key

6.

What does the following statement do?

purse['candy'] = purse['candy'] + 2

a)

Adds 2 new candies

b)

Increases the value of 'candy' key by 2

7.

What is the output of this code?

ddd = {}

ddd['age'] = 21

ddd['course'] = 182

print(ddd)

a)

A. {21: 'age', 182: 'course'}

b)

B. {'age': 21, 'course': 182}

c)

C. {182, 21}

d)

D. {'ddd': 'dict'}

8.

What is the purpose of the get() method in a dictionary?

a)

To remove a key-value pair

b)

To retrieve a value safely without an error if key doesn’t exist

c)

To create a new dictionary

d)

To get all keys

9.

What is the output of this statement?

counts.get('zqian', 0)

a)

Returns 0 if 'zqian' key does not exist

b)

Deletes the key 'zqian'

c)

Adds 0 to the dictionary

d)

Raises a KeyError

10.

In the statement

counts[name] = counts.get(name, 0) + 1,

why is +1 used?

a)

A. To count how many times the word appears

b)

B. To check if the key exists

c)

C. To reset the count

d)

D. To create a new list

11.

What is the output of the following?

jjj = {'chuck': 1, 'fred': 42, 'jan': 100}

print(jjj.keys())

a)

A. ['chuck', 'fred', 'jan']

b)

B. [1, 42, 100]

c)

C. [('chuck', 1), ('fred', 42), ('jan', 100)]

d)

D. ['jjj']

12.

The method that returns both keys and values together is:

a)

items()

b)

keys()

c)

values()

d)

get()

13.

What does the following loop do?

for key in counts:

print(key, counts[key])

a)

Loops through all dictionary values only

b)

Loops through all keys and prints each value

c)

Prints dictionary name only

d)

Causes an error

14.

In a dictionary, keys must be:

a)

Mutable objects

b)

Immutable objects

c)

Numbers only

d)

Strings only

15.

What will this code display?
words = ['the', 'car', 'the', 'car']
counts = {}
for word in words:
    counts[word] = counts.get(word, 0) + 1
print(counts)

a)

A. {'the': 2, 'car': 2}

b)

B. {'the': 1, 'car': 1}

c)

C. {'the': 2, 'car': 1}

d)

D. {'words': 4}

16.

The function split() is used to:

a)

Join strings together

b)

Break a line of text into words

c)

Sort dictionary items

d)

Count the words

17.

What does .values() return?

a)

All dictionary keys

b)

All dictionary values

c)

Both keys and values

d)

The number of items

18.

What is the output of the following code?

ijj = {'chuck': 1, 'fred': 42, 'jan': 100}

for aaa, bbb in ijj.items():

print(aaa, bbb)

a)

A. Prints only keys

b)

B. Prints only values

c)

C. Prints keys and values together

d)

D. Error due to unpacking

19.

What is the correct syntax in Python 3 for printing key and value?

a)

print aaa, bbb

b)

print(aaa, bbb)

c)

print[aaa, bbb]

d)

echo(aaa, bbb)

20.

Which statement about dictionaries is TRUE?

a)

Dictionaries store values in order

b)

Dictionaries are slower than lists

c)

Dictionaries use keys to access values

d)

Dictionaries can have duplicate keys

21.

A dictionary can store multiple values for the same key.

a)

True

b)

False

22.

A list stores data by position, while a dictionary stores data by key.

a)

True

b)

False

23.

You can use curly braces {} to create both lists and dictionaries.

a)

True

b)

False

24.

Using .get() prevents a KeyError when accessing a missing key.

a)

True

b)

False

25.

The +1 in counts[word] = counts.get(word, 0) + 1 is used to increment the count.

a)

True

b)

False

26.

Dictionaries maintain insertion order in all Python versions.

a)

True

b)

False

27.

The .items() method returns a list of tuples containing key-value pairs.

a)

True

b)

False

28.

In a for loop, for key, value in dict.items(): gives access to both key and value.

a)

True

b)

False

29.

Dictionary keys must always be integers.

a)

True

b)

False

30.

A dictionary can be used to count how many times each word appears in a sentence.

a)

True

b)

False