Ch9 Dictionaries And Sets Complete Test Bank - Test Bank | Python 5e by Gaddis by Tony Gaddis. DOCX document preview.
Starting Out with Python 5e (Gaddis)
Chapter 9 Dictionaries and Sets
TRUE/FALSE
1. You would typically use a for loop to iterate over the elements in a set.
2. Sets are immutable.
3. Sets are created using curly braces { }.
4. The set remove and discard methods behave differently only when a specified item is not found in the set.
5. A dictionary can include the same value several times but cannot include the same key several times.
6. The union of two sets is a set that contains only the elements that appear in both sets.
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.
8. The elements in a dictionary are stored in ascending order, by the keys of the key-value pairs.
9. If you try to retrieve a value from a dictionary using a nonexistent key, a KeyError exception is raised.
10. The issubset() method can be used to determine whether set1 is a subset of set2.
11. A set comprehension is written just like a list comprehension, except that a set comprehension is enclosed in angled brackets (<>), and a list comprehension is enclosed in square brackets ([]).
MULTIPLE CHOICE
1. In a dictionary, you use a(n) __________ to locate a specific value.
a. | datum |
b. | element |
c. | item |
d. | key |
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' } |
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 |
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. |
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. |
6. Which would you use to delete an existing key-value pair from a dictionary?
a. | del |
b. | remove |
c. | delete |
d. | unpair |
7. Which would you use to get the number of elements in a dictionary?
a. | size |
b. | length |
c. | len |
d. | sizeof |
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 |
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 |
10. What values will d2 contain after the following code executes?
d = {1: 10, 2: 20, 3: 30}
d2 = {k:v for k,v in d.items()}
a. {10: 1, 20: 2, 30: 3}
b. {1: 1, 2: 2, 3: 3}
c. {10: 10, 20: 20, 30: 30}
d. {1: 10, 2: 20, 3: 30}
11. Which method can be used to add a group of elements to a set?
a. | add |
b. | addgroup |
c. | update |
d. | addset |
12. 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 |
13. 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. |
14. 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. |
15. 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 |
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 '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'} |
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. | {'CA': 'San Diego', 'NY': 'Albany', 'GA': 'Atlanta', 'FL' 'Tallahassee'} |
d. | {'CA': 'San Diego', 'NY': 'Albany', 'GA': 'Atlanta'} |
18. 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'} |
COMPLETION
1. A(n) __________ is an object that holds multiple unique items of data in an unordered manner.
2. The built-in function, __________, returns the number of items in a set.
3. To add a single item to a set, you can use the set __________ method.
4. The __________ of two sets is a set that contains all the elements of both sets.
5. Each element in a(n) __________ has two parts: a key and a value.
6. The elements in a dictionary are not stored in a specific order. Therefore, a dictionary is not a(n) ___________.
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.
8. The __________ method returns a value associated with a specific key and, if found, removes that key-value pair from the dictionary.
9. The __________ method clears the contents of a dictionary.
10. To write an object to a file, you use the __________ function of the __________ module.
11. The __________ method returns all of a dictionary's keys as a dictionary view.
12. Each element in a dictionary view is a __________.