NEW
Font size
WorksheetsChapter 9: Dictionaries
Total questions: 30
Worksheet time: 23mins
What is the main difference between a list and a dictionary in Python?
A. Lists store data in pairs of key and value
B. Dictionaries store data in pairs of key and value
C. Lists can only store numbers
D. Dictionaries can only store strings
How do you create an empty dictionary?
A. dict = []
B. dict = ()
C. dict = {}
D. dict = ''
What will this code display?
student = {'name': 'Ali', 'age': 20}
print(student['name'])
name
Ali
age
student
Which symbol is used to define a dictionary literal?
Square brackets [ ]
Curly braces { }
Parentheses ( )
Angle brackets < >
What will happen if you try to access a key that doesn’t exist in a dictionary?
Returns 0
Returns None
Raises a KeyError
Skips the key
What does the following statement do?
purse['candy'] = purse['candy'] + 2
Adds 2 new candies
Increases the value of 'candy' key by 2
What is the output of this code?
ddd = {}
ddd['age'] = 21
ddd['course'] = 182
print(ddd)
A. {21: 'age', 182: 'course'}
B. {'age': 21, 'course': 182}
C. {182, 21}
D. {'ddd': 'dict'}
What is the purpose of the get() method in a dictionary?
To remove a key-value pair
To retrieve a value safely without an error if key doesn’t exist
To create a new dictionary
To get all keys
What is the output of this statement?
counts.get('zqian', 0)
Returns 0 if 'zqian' key does not exist
Deletes the key 'zqian'
Adds 0 to the dictionary
Raises a KeyError
In the statement
counts[name] = counts.get(name, 0) + 1,
why is +1 used?
A. To count how many times the word appears
B. To check if the key exists
C. To reset the count
D. To create a new list
What is the output of the following?
jjj = {'chuck': 1, 'fred': 42, 'jan': 100}
print(jjj.keys())
A. ['chuck', 'fred', 'jan']
B. [1, 42, 100]
C. [('chuck', 1), ('fred', 42), ('jan', 100)]
D. ['jjj']
The method that returns both keys and values together is:
items()
keys()
values()
get()
What does the following loop do?
for key in counts:
print(key, counts[key])
Loops through all dictionary values only
Loops through all keys and prints each value
Prints dictionary name only
Causes an error
In a dictionary, keys must be:
Mutable objects
Immutable objects
Numbers only
Strings only
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. {'the': 2, 'car': 2}
B. {'the': 1, 'car': 1}
C. {'the': 2, 'car': 1}
D. {'words': 4}
The function split() is used to:
Join strings together
Break a line of text into words
Sort dictionary items
Count the words
What does .values() return?
All dictionary keys
All dictionary values
Both keys and values
The number of items
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. Prints only keys
B. Prints only values
C. Prints keys and values together
D. Error due to unpacking
What is the correct syntax in Python 3 for printing key and value?
print aaa, bbb
print(aaa, bbb)
print[aaa, bbb]
echo(aaa, bbb)
Which statement about dictionaries is TRUE?
Dictionaries store values in order
Dictionaries are slower than lists
Dictionaries use keys to access values
Dictionaries can have duplicate keys
A dictionary can store multiple values for the same key.
True
False
A list stores data by position, while a dictionary stores data by key.
True
False
You can use curly braces {} to create both lists and dictionaries.
True
False
Using .get() prevents a KeyError when accessing a missing key.
True
False
The +1 in counts[word] = counts.get(word, 0) + 1 is used to increment the count.
True
False
Dictionaries maintain insertion order in all Python versions.
True
False
The .items() method returns a list of tuples containing key-value pairs.
True
False
In a for loop, for key, value in dict.items(): gives access to both key and value.
True
False
Dictionary keys must always be integers.
True
False
A dictionary can be used to count how many times each word appears in a sentence.
True
False
