Deck 9: Dictionaries and Sets
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/35
Play
Full screen (f)
Deck 9: Dictionaries and Sets
1
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.
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.
A
2
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
A) included
B) of
C) in
D) not in
C
3
Sets are immutable.
False
4
Which function would you use to get the number of elements in a dictionary?
A) size
B) length
C) len
D) invalid code
A) size
B) length
C) len
D) invalid code
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
5
In a dictionary, you use a(n) _____ to locate a specific value.
A) datum
B) element
C) item
D) key
A) datum
B) element
C) item
D) key
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
6
You would typically use a for loop to iterate over the elements in a set.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
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
Phones = {'John': '5555555', 'Julie' : '7777777'}
Phones['John'] = '1234567'
A) {'John': '5555555', 'Julie' : '7777777'}
B) {'John': '1234567', 'Julie' : '7777777'}
C) {'John': '1234567' }
D) invalid code
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
8
Which statement would you use to delete an existing key-value pair from a dictionary?
A) del
B) remove
C) delete
D) unpair
A) del
B) remove
C) delete
D) unpair
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
9
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
A) list
B) items
C) pop
D) keys
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
10
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.
A) add
B) addgroup
C) update
D) Elements must be added one at a time.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
11
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
Ages = {'Aaron' : 6, 'Kelly' : 3, 'Abigail' : 1 }
Value = ages['Brianna']
A) false
B) -1
C) 0
D) KeyError
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
12
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.
A) 0
B) 1
C) Dictionary is not indexed by number.
D) Size of the dictionary minus one.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
13
The set remove and discard methods behave differently only when a specified item is not found in the set.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
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 - no two elements can have the same value.
C) The elements are unordered.
D) The elements are pairs.
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.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
15
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' }
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' }
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
16
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
A) Pickling
B) Streaming
C) Writing
D) Dumping
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
17
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
A) list
B) items
C) popitem
D) pop
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
18
Sets are created using curly braces {}.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
19
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
A) Throw an exception
B) Nothing
C) Return a default value
D) You do not specify a key for the get method
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
20
A dictionary can include the same value several times, but cannot include the same key several times.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
21
A(n) _______________ is an object that holds multiple unique items of data in an unordered manner.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
22
The issubset method can be used to determine whether set1 is a subset of set2.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
23
The _______________ of two sets is a set that contains all the elements of both sets.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
24
The _______________ method clears the contents of a dictionary.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
25
The built-in function _______________ returns the number of items in a set.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
26
The _______________ method returns a value associated with a specified key and if found, removes that key-value pair from the dictionary.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
27
The elements in a dictionary are not stored in a specific order. Therefore a dictionary is not a(n) _______________.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
28
The union of two sets is a set that contains only the elements that appear in both sets.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
29
Each element in a(n) _______________ has two parts: a key and a value.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
30
The difference of set1 and set2 is a set that contains only the elements that appear in set1 but do not appear in set2.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
31
The index of the first element in a set is 0.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
32
To add a single item to a set, you can use the set method _______________.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
33
To write an object to a file, you use the _______________ function of the _______________ module.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
34
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.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
35
The index of the first key-value pair in a dictionary is 0.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck