Gaddis Python 2e Chapter 10
Download file with the answers
Gaddis Python 2e Chapter 10
1 file(s) 127.42 KB
If you are not a member register here to download this file
Chapter Ten
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 for creating a dictionary of month names to be accessed by month numbers?
a. { 1 ; ‘January’, 2 ; ‘February’, 3 ; ‘March’ }
b. { 1 : ‘January’, 2 : ‘February’, 3 : ‘March’ }
c. [ 1 : ‘January’, 2 : ‘February’, 3 : ‘March’ ]
d. { 1, 2, 3 : ‘January’, ‘February’, ‘March’ }
ANS: B
3. What would 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 list dictionary?
a. 0
b. 1
c. Dictionary is not indexed by number.
d. Size of the dictionary minus one.
ANS: C
5. You created the following dictionary relationships = {‘Jimmy’:’brother’}. You then executed the following code, and received a KeyError exception. What is the reason for the exception?
relationships[‘jimmy’]
a. String comparisons are case sensitive so ‘jimmy’ does not equal ‘Jimmy’.
b. You used the wrong syntax for creating the dictionary.
c. You should have used the code relationships[‘brother’].
d. There is a bug in Python.
ANS: A
6. In order to avoid KeyError exceptions, you can check whether a key is in the dictionary using the _____ operator.
a. included
b. of
c. in
d. not in
ANS: C
7. What is the value of the variable phones after the execution of the following code?
phones = {‘John’: ‘5555555’, ‘Julie’ : ‘7777777’}
phones[‘John’] = ‘1234567’
a. {‘John’: ‘5555555’, ‘Julie’ : ‘7777777’}
b. {‘John’: ‘1234567’, ‘Julie’ : ‘7777777’}
c. {‘John’: ‘1234567’ }
d. invalid code
ANS: B
8. Which statement would you use to delete an existing key-value pair from a dictionary?
a. del
b. remove
c. delete
d. unpair
ANS: A
9. Which function would you use to get the number of elements in a dictionary?
a. size
b. length
c. len
d. invalid code
ANS: C
10. Which method would you use to returns all the elements in the dictionary as a list of tuples?
a. list
b. items
c. pop
d. keys
ANS: B
11. Which method would you use to return the value associated with a specified key and remove that key-value pair from the dictionary?
a. list
b. items
c. popitem
d. pop
ANS: D
12. What does the get method do if the specified key is not found in the dictionary?
a. Throw an exception
b. Nothing
c. Return a default value
d. You do not specify a key for the get method
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 – no two elements can have the same value.
c. The elements are unordered.
d. The elements are pairs.
ANS: D
14. What method can be used to add a group of elements to a set?
a. add
b. addgroup
c. update
d. Elements must be added one at a time.
ANS: C
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
ANS: A
TRUE/FALSE
1. True/False: You would typically use a for loop to iterate over the elements in a set.
ANS: T
2. True/False: Sets are immutable.
ANS: F
3. True/False: Sets are created using curly braces {}.
ANS: F
4. True/False: The set remove and discard methods behave differently only when a specified item is not found in the set.
ANS: T
5. True/False: A dictionary can include the same value several times, but cannot include the same key several times.
ANS: T
6. True/False: The union of two sets is a set that contains only the elements that appear in both sets.
ANS: F
7. True/False: The difference of set1 and set2 is a set that contains only the elements that appear in set1 but do not appear in set2.
ANS: T
8. True/False: The index of the first element in a set is 0.
ANS: F
9. True/False: The index of the first key-value pair in a dictionary is 0.
ANS: F
10. True/False: The issubset method can be used to determine whether set1 is a subset of set2.
ANS: T
FILL IN THE BLANK
1. A(n) _______________ is an object that holds multiple unique items of data in an unordered manner.
ANS: set
2. The _______________ of two sets is a set that contains all the elements of both sets.
ANS: union
3. The built-in function _______________ returns the number of items in a set.
ANS: len
4. To add a single item to a set, you can use the set method _______________.
ANS: add
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 a key is not included in a dictionary, or an element is not included in a set, you can use the _______________ operator.
ANS: not in
8. The _______________ method returns a value associated with a specified 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
Leave a reply