Week 9 Flashcards
You would typically use a for loop to iterate over the elements in a set.
True
Sets are immutable.
False
Sets are created using curly braces { }.
False
The set remove and discard methods behave differently only when a specified item is not found in the set.
True
A dictionary can include the same value several times but cannot include the same key several times.
True
The union of two sets is a set that contains only the elements that appear in both sets.
False
The difference of set1 and aet2 is a set that contains only the elements that appear in set1 but do not appear in set2.
True
The elements in a dictionary are stored in ascending order, by the keys of the key-value pairs.
False
If you try to retrieve a value from a dictionary using a nonexistent key, a KeyError exception is raised.
True
The issubset() method can be used to determine whether set1 is a subset of set2.
True
In a dictionary, you use a(n) __________ to locate a specific value.
a. datum
b. element
c. item
d. key
d. key
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’ }
b. { 1 : ‘January’, 2 : ‘February’, … 12 : ‘December’ }
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
d. KeyError
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.
d. Dictionaries are not indexed by number.
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.
b. {‘John’ : ‘5556666’, ‘Julie’ : ‘5557777’}
Which would you use to delete an existing key-value pair from a dictionary?
a. del
b. remove
c. delete
d. unpair
a. del
Which would you use to get the number of elements in a dictionary?
a. size
b. length
c. len
d. sizeof
c. len
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
b. items
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
c. pop
Which method can be used to add a group of elements to a set?
a. add
b. addgroup
c. update
d. addset
c. update
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
b. in
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.
c. It returns a default value.
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.
d. The elements are in pairs.
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
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’}
{‘GA’: ‘Atlanta’, ‘NY’: ‘Albany’, ‘CA’: ‘Sacramento’}
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’}
{‘GA’: ‘Atlanta’, ‘NY’: ‘Albany’, ‘CA’: ‘San Diego’}
A(n) __________ is an object that holds multiple unique items of data in an unordered manner.
set
The built-in function, __________, returns the number of items in a set.
len
To add a single item to a set, you can use the set __________ method.
add
The __________ of two sets is a set that contains all the elements of both sets.
union
Each element in a(n) __________ has two parts: a key and a value.
dictionary
The elements in a dictionary are not stored in a specific order. Therefore, a dictionary is not a(n) ___________.
sequence
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.
not in
The __________ method returns a value associated with a specific key and, if found, removes that key-value pair from the dictionary.
pop
The __________ method clears the contents of a dictionary.
clear
To write an object to a file, you use the __________ function of the __________ module.
dump, pickle
The __________ method returns all of a dictionary’s keys as a dictionary view.