-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathChapter 5_Python quiz.py
More file actions
184 lines (100 loc) · 3.07 KB
/
Chapter 5_Python quiz.py
File metadata and controls
184 lines (100 loc) · 3.07 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
1.Question 1
What is wrong with this Python loop:
n = 5
while n > 0 :
print(n)
print('All done')
<A> This loop will run forever
<B> The print('All done') statement should be indented four spaces
<C> There should be no colon on the while statement
<D> while is not a Python reserved word
AnS: <A> This loop will run forever
2.Question 2
What does the break statement do?
<A> Exits the currently executing loop
<B> Exits the program
<C> Resets the iteration variable to its initial value
<D> Jumps to the "top" of the loop and starts the next iteration
AnS:<A> Exits the currently executing loop
3.Question 3
What does the continue statement do?
<A> Jumps to the "top" of the loop and starts the next iteration
<B> Resets the iteration variable to its initial value
<C> Exits the program
<D> Exits the currently executing loop
AnS: <A> Jumps to the "top" of the loop and starts the next iteration
4.Question 4
What does the following Python program print out?
tot = 0
for i in [5, 4, 3, 2, 1] :
tot = tot + 1
print(tot)
<A> 10
<B> 5
<C> 0
<D> 15
AnS: <B> 5
5.Question 5
What is the iteration variable in the following Python code:
friends = ['Joseph', 'Glenn', 'Sally']
for friend in friends :
print('Happy New Year:', friend)
print('Done!')
<A> friend
<B> Sally
<C> Glenn
<D> Joseph
AnS: <A> friend
6.Question 6
What is a good description of the following bit of Python code?
friends = ['Joseph', 'Glenn', 'Sally']
for friend in friends :
print('Happy New Year:', friend)
print('Done!')
<A> Count all of the elements in a list
<B> Find the smallest item in a list
<C> Sum all the elements of a list
<D> Compute the average of the elements in a list
AnS: <C> Sum all the elements of a list
7.Question 7
What will the following code print out?
smallest_so_far = -1
for the_num in [9, 41, 12, 3, 74, 15] :
if the_num < smallest_so_far :
smallest_so_far = the_num
print(smallest_so_far)
Hint: This is a trick question and most would say this code has a bug - so read carefully
<A> -1
<B> 74
<C> 42
<D> 3
AnS: <A> -1
8.Question 8
What is a good statement to describe the is operator as used in the following if statement:
if smallest is None :
smallest = value
<A> The if statement is a syntax error
<B> Looks up 'None' in the smallest variable if it is a string
<C> Is true if the smallest variable has a value of -1
<D> matches both type and value
AnS: <B> not Looks up 'None' in the smallest variable if it is a string orifThe if statement is a syntax error
9.Question 9
Which reserved word indicates the start of an "indefinite" loop in Python?
<A> break
<B> def
<C> for
<D> while
<E> indef
AnS: <D> while
10.Question 10
How many times will the body of the following loop be executed?
n = 0
while n > 0 :
print('Lather')
print('Rinse')
print('Dry off!')
<A> This is an infinite loop
<B> 5
<C> 0
<D> 1
AnS: <A> this is an indefinite loop