Starting Out with Python 4e (Gaddis)
Chapter 9 Dictionaries and Sets
TRUE/FALSE
1. You would typically use a for loop to iterate over the elements in a set.
ANS: T
2. Sets are immutable.
ANS: F
3. Sets are created using curly braces { }.
ANS: F
4. The set remove and discard methods behave differently only when a specified item is not found in the set.
ANS: T
5. A dictionary can include the same value several times but cannot include the same key several times.
ANS: T
6. The union of two sets is a set that contains only the elements that appear in both sets.
ANS: F
7. The difference of set1 and aet2 is a set that contains only the elements that appear in set1 but do not appear in set2.
ANS: T
8. The elements in a dictionary are stored in ascending order, by the keys of the key-value pairs.
ANS: F
9. If you try to retrieve a value from a dictionary using a nonexistent key, a KeyError exception is raised.
ANS: T
10. The issubset() method can be used to determine whether set1 is a subset of set2.
ANS: T
MULTIPLE CHOICE
1. In a dictionary, you use a(n) __________ to locate a specific value.
a.
datum
b.
element
c.
item
d.
key
ANS: D
2. What is the correct structure to create a dictionary of months where each month will be accessed by its month number (for example, January is month 1, April is month 4)?
a.
{ 1 ; ‘January’, 2 ; ‘February’, … 12 ; ‘December’}
b.
{ 1 : ‘January’, 2 : ‘February’, … 12 : ‘December’ }
c.
[ ‘1’ : ‘January’, ‘2’ : ‘February’, … ’12’ : ‘December’ ]
d.
{ 1, 2,… 12 : ‘January’, ‘February’,… ‘December’ }
ANS: B
3. What will be the result of the following code?
ages = {‘Aaron’ : 6, ‘Kelly’ : 3, ‘Abigail’ : 1 }
value = ages[‘Brianna’]
a.
False
b.
-1
c.
0
d.
KeyError
ANS: D
4. What is the number of the first index in a dictionary?
a.
0
b.
1
c.
the size of the dictionary minus one
d.
Dictionaries are not indexed by number.
ANS: D
5. What is the value of the variable phones after the following code executes?
phones = {‘John’ : ‘5555555’, ‘Julie’ : ‘5557777’}
phones[‘John’] = 5556666′
a.
{‘John’ : ‘5555555’, ‘Julie’ : ‘5557777’}
b.
{‘John’ : ‘5556666’, ‘Julie’ : ‘5557777’}
c.
{‘John’ : ‘5556666’}
d.
This code is invalid.
ANS: B
6. Which would you use to delete an existing key-value pair from a dictionary?
a.
del
b.
remove
c.
delete
d.
unpair
ANS: A
7. Which would you use to get the number of elements in a dictionary?
a.
size
b.
length
c.
len
d.
sizeof
ANS: C
8. Which method would you use to get all the elements in a dictionary returned as a list of tuples?
a.
list
b.
items
c.
pop
d.
keys
ANS: B
9. Which method would you use to get the value associated with a specific key and remove that key-value pair from the dictionary?
a.
list
b.
items
c.
pop
d.
popitem
ANS: C
10. Which method can be used to add a group of elements to a set?
a.
add
b.
addgroup
c.
update
d.
addset
ANS: C
11. In order to avoid KeyError exceptions, you can check whether a key is in the dictionary using the __________ operator.
a.
included
b.
in
c.
isnotin
d.
isin
ANS: B
12. What does the get method do if the specified key is not found in the dictionary?
a.
It throws an exception.
b.
It does nothing.
c.
It returns a default value.
d.
You cannot use the get method to specify a key.
ANS: C
13. Which of the following does not apply to sets?
a.
The stored elements can be of different data types.
b.
All the elements must be unique; you cannot have two elements with the same value.
c.
The elements are unordered.
d.
The elements are in pairs.
ANS: D
14. What is the process used to convert an object to a stream of bytes that can be saved in a file?
a.
pickling
b.
streaming
c.
writing
d.
dumping
ANS: A
15. What will be displayed after the following code executes? (Note: the order of the display of entries in a dictionary are not in a specific order.)
cities = {‘GA’ : ‘Atlanta’, ‘NY’ : ‘Albany’, ‘CA’ : ‘San Diego’}
if ‘CA’ in cities:
del cities[‘CA’]
cities[‘CA’] = ‘Sacramento’
print(cities)
a.
{‘CA’: ‘Sacramento’}
b.
[‘CA’: ‘Sacramento’]
c.
{‘NY’: ‘Albany’, ‘GA’: ‘Atlanta’}
d.
{‘CA’: ‘Sacramento’, ‘NY’: ‘Albany’, ‘GA’: ‘Atlanta’}
ANS: D
16. What will be displayed after the following code executes? (Note: the order of the display of entries in a dictionary are not in a specific order.)
cities = {‘GA’ : ‘Atlanta’, ‘NY’ : ‘Albany’, ‘CA’ : ‘San Diego’}
if ‘FL’ in cities:
del cities[‘FL’]
cities[‘FL’] = ‘Tallahassee’
print(cities)
a.
{‘FL’: ‘Tallahassee’}
b.
KeyError
c.
{‘CA’: ‘San Diego’, ‘NY’: ‘Albany’, ‘GA’: ‘Atlanta’, ‘FL’ ‘Tallahassee’}
d.
{‘CA’: ‘San Diego’, ‘NY’: ‘Albany’, ‘GA’: ‘Atlanta’}
ANS: D
17. What will be displayed after the following code executes? (Note: the order of the display of entries in a dictionary are not in a specific order.)
cities = {‘GA’ : ‘Atlanta’, ‘NY’ : ‘Albany’, ‘CA’ : ‘San Diego’}
if ‘FL’ in cities:
del cities[‘FL’]
cities[‘FL’] = ‘Tallahassee’
print(cities)
a.
{‘FL’: ‘Tallahassee’}
b.
KeyError
c.
{‘GA’: ‘Atlanta’, ‘FL’: ‘Tallahassee’, ‘NY’: ‘Albany’, ‘CA’: ‘San Diego’}
d.
{‘CA’: ‘San Diego’, ‘NY’: ‘Albany’, ‘GA’: ‘Atlanta’}
ANS: C
COMPLETION
1. A(n) __________ is an object that holds multiple unique items of data in an unordered manner.
ANS: set
2. The built-in function, __________, returns the number of items in a set.
ANS: len
3. To add a single item to a set, you can use the set __________ method.
ANS: add
4. The __________ of two sets is a set that contains all the elements of both sets.
ANS: union
5. Each element in a(n) __________ has two parts: a key and a value.
ANS: dictionary
6. The elements in a dictionary are not stored in a specific order. Therefore, a dictionary is not a(n) ___________.
ANS: sequence
7. To determine whether or not a key is included in a dictionary, or if an element is included in a set, you can use the ___________ operator.
ANS: not in
8. The __________ method returns a value associated with a specific key and, if found, removes that key-value pair from the dictionary.
ANS: pop
9. The __________ method clears the contents of a dictionary.
ANS: clear
10. To write an object to a file, you use the __________ function of the __________ module.
ANS: dump, pickle
11. The __________ method returns all of a dictionary’s keys as a dictionary view.
ANS: keys
12. Each element in a dictionary view is a __________.
ANS: tuple