This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
forked from ed-lau/python-for-everybody
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wk9 - quiz.py
127 lines (105 loc) · 2.89 KB
/
wk9 - quiz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
__author__ = 'edwardlau'
"""
Question 1
How are Python dictionaries different from Python lists?
Python lists are indexed using integers and dictionaries can use strings as indexes
Python dictionaries are a collection and lists are not a collection
Python lists can store strings and dictionaries can only store words
Python lists store multiple values and dictionaries store a single value
"""
# Answer: Python lists are indexed using integers and dictionaries can use strings as indexes
"""
Question 2
What is a term commonly used to describe the Python dictionary feature in other programming languages?
Closures
Sequences
Associative arrays
Lambdas
"""
# Answer: Associative arrays
"""
Question 3
What would the following Python code print out?
"""
stuff = dict()
print stuff['candy']
"""
The program would fail with a traceback
0
-1
candy
"""
# Answer: The program would fail with a traceback
"""
Question 4
What would the following Python code print out?
stuff = dict()
print stuff.get('candy',-1)
The program would fail with a traceback
'candy'
0
-1
"""
# Answer: -1
"""
Question 5
(T/F) When you add items to a dictionary they remain in the order in which you added them.
False
True
"""
# Answer: False
"""
Question 6
What is a common use of Python dictionaries in a program?
Computing an average of a set of numbers
Sorting a list of names into alphabetical order
Building a histogram counting the occurrences of various strings in a file
Splitting a line of input into words using a space as a delimiter
"""
# Answer: Building a histogram counting the occurrences of various strings in a file
"""
Question 7
Which of the following lines of Python is equivalent to the following sequence of statements assuming that counts is a dictionary?
if key in counts:
counts[key] = counts[key] + 1
else:
counts[key] = 1
counts[key] = counts.get(key,0) + 1
counts[key] = counts.get(key,-1) + 1
counts[key] = (key in counts) + 1
counts[key] = key + 1
counts[key] = (counts[key] * 1) + 1
"""
# Answer: counts[key] = counts.get(key,0) + 1
"""
Question 8
In the following Python, what does the for loop iterate through?
x = dict()
...
for y in x :
...
It loops through the integers in the range from zero through the length of the dictionary
It loops through all of the dictionaries in the program
It loops through the values in the dictionary
It loops through the keys in the dictionary
"""
# Answer: It loops through the keys in the dictionary
"""
Question 9
Which method in a dictionary object gives you a list of the values in the dictionary?
items()
keys()
values()
all()
each()
"""
# Answer: values()
"""
Question 10
What is the purpose of the second parameter of the get() method for Python dictionaries?
An alternate key to use if the first key cannot be found
The value to retrieve
To provide a default value if the key is not found
The key to retrieve
"""
# Answer: To provide a default value if the key is not found