Week 9 Flashcards

1
Q

You would typically use a for loop to iterate over the elements in a set.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Sets are immutable.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Sets are created using curly braces { }.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The set remove and discard methods behave differently only when a specified item is not found in the set.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

A dictionary can include the same value several times but cannot include the same key several times.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The union of two sets is a set that contains only the elements that appear in both sets.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

The difference of set1 and aet2 is a set that contains only the elements that appear in set1 but do not appear in set2.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The elements in a dictionary are stored in ascending order, by the keys of the key-value pairs.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

If you try to retrieve a value from a dictionary using a nonexistent key, a KeyError exception is raised.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

The issubset() method can be used to determine whether set1 is a subset of set2.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

In a dictionary, you use a(n) __________ to locate a specific value.
a. datum
b. element
c. item
d. key

A

d. key

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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’ }

A

b. { 1 : ‘January’, 2 : ‘February’, … 12 : ‘December’ }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

A

d. KeyError

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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.

A

d. Dictionaries are not indexed by number.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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.

A

b. {‘John’ : ‘5556666’, ‘Julie’ : ‘5557777’}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Which would you use to delete an existing key-value pair from a dictionary?
a. del
b. remove
c. delete
d. unpair

A

a. del

17
Q

Which would you use to get the number of elements in a dictionary?
a. size
b. length
c. len
d. sizeof

A

c. len

18
Q

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

A

b. items

19
Q

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

A

c. pop

20
Q

Which method can be used to add a group of elements to a set?
a. add
b. addgroup
c. update
d. addset

A

c. update

21
Q

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

A

b. in

22
Q

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.

A

c. It returns a default value.

23
Q

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.

A

d. The elements are in pairs.

24
Q

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

a. pickling

25
Q

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’}

A

{‘GA’: ‘Atlanta’, ‘NY’: ‘Albany’, ‘CA’: ‘Sacramento’}

26
Q

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’}

A

{‘GA’: ‘Atlanta’, ‘NY’: ‘Albany’, ‘CA’: ‘San Diego’}

27
Q

A(n) __________ is an object that holds multiple unique items of data in an unordered manner.

A

set

28
Q

The built-in function, __________, returns the number of items in a set.

A

len

29
Q

To add a single item to a set, you can use the set __________ method.

A

add

30
Q

The __________ of two sets is a set that contains all the elements of both sets.

A

union

31
Q

Each element in a(n) __________ has two parts: a key and a value.

A

dictionary

32
Q

The elements in a dictionary are not stored in a specific order. Therefore, a dictionary is not a(n) ___________.

A

sequence

33
Q

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.

A

not in

34
Q

The __________ method returns a value associated with a specific key and, if found, removes that key-value pair from the dictionary.

A

pop

35
Q

The __________ method clears the contents of a dictionary.

A

clear

36
Q

To write an object to a file, you use the __________ function of the __________ module.

A

dump, pickle

37
Q

The __________ method returns all of a dictionary’s keys as a dictionary view.

A