Skip to content

Commit 6cad4d2

Browse files
committed
quiz of chap 9
1 parent 84d9cbe commit 6cad4d2

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

Chap_9_Quiz.py

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

0 commit comments

Comments
 (0)