-
Notifications
You must be signed in to change notification settings - Fork 0
/
wk10 - final exam.py
298 lines (257 loc) · 4.57 KB
/
wk10 - final exam.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
__author__ = 'edwardlau'
"""
Question 1
What will the following Python program print out:
def fred():
print "Zap"
def jane():
print "ABC"
fred()
jane()
fred()
"""
#Answer: Zap ABC Zap
"""
Question 2
What would the following Python code sequence print?
str = "hello there bob"
print str[0]
there
hello there bob
h
It would fail with an index error
"""
#Answer: h
"""
Question 3
What part of a computer is actually doing the addition and subtraction in the following statement:
x = 1 + 2 - 3 * 4 / 5 + 6
Central Processing Unit
Network Controller
Input Devices
Cloud
"""
#Answer: Central Processing Unit
"""
Question 4
Which of the following lines will never print out regardless of the value for x?
if x < 2 :
print "Below 2"
elif x < 0 :
print "Negative"
else :
print "Something else"
All three lines will print all the time
Negative
Something else
Below 2
"""
#Answer: Negative
"""
Question 5
What will the following code print out?
x = 12
if x <= 10:
if x > 4:
print "One"
else:
print "Two"
else:
if x >= 11:
print "Three"
else:
print "Four"
One
Three
Two
Four
"""
#Answer: Three
"""
Question 6
What would the following Python print out?
abc = "With three words"
stuff = abc.split()
print len(stuff)
['With three words']
14
16
['With', 'three', 'words']
2
3
6
"""
#Answer: 3
"""
Question 7
What would the value of the following expression be:
abc = 1 + 2 - 3 * 4 + 5 - 6 / 3
36
0.9675
42
-6
"""
#Answer: -6
"""
Question 8
What is the primary use of the Python dictionary?
To make sure that the definitions of the Python reserved words are available in different languages (French, Spanish, etc)
To insure that all Python reserved words are properly spelled
To store key / value pairs
To store items in order (like a can of Pringles potato chips)
"""
#Answer: To store key / value pairs
"""
Question 9
What will the following Python program print out:
def fred():
print "Zap"
def jane():
print "ABC"
jane()
fred()
jane()
"""
#Answer: ABC Zap ABC
"""
Question 10
Which of these is not one of the four types of control flow used in programs:
Conditional
Repeated
Store and Reuse
Fused / Multi-Path
"""
Answer: Fused/ Multi-Path
"""
Question 11
What would happen if the following Python code were executed?
st = "abc"
ix = int(st)
The variable ix would contain 0
The variable ix would contain None
The program would show an error and a traceback on the second line
The variable ix would contain -1
"""
# Answer: The program would show an error and a traceback on the second line
"""
Question 12
What would this code print out?
lst = []
lst.append(4)
lst.append(10)
lst.append(21)
lst.append(6)
print lst[2]
none of the above
21
10
4
"""
#Answer: 21
"""
Question 13
If all of the following were used in a Python expression, which would have the highest precedence (i.e. which would be evaluated first)?
Subtraction
Modulo (i.e. remainder)
Parenthesis
Addition
"""
# Answer: Parenthesis
"""
Question 14
If you want your program to recover from errors that would otherwise cause a trace back and your program to stop, what language element of Python would you use?
try / except
on_error / goto
repeat / on_error
when / error
"""
# Answer: try/except
"""
Question 15
You develop the following program in Python:
f = int(raw_input("Enter:"))
c = ( f - 32 ) * ( 5 / 9 )
print "Celsius",c
And when you run it three times you get the following output:
Enter:212
Celsius 0
Enter:72
Celsius 0
Enter:15
Celsius 0
What part of the program is causing the output to always be zero?
( f - 32 )
Using single character variables
Using double quotes for all the strings
( 5 / 9 )
"""
#Answer: (5/9)
"""
Question 16
For the following Python program, what will it print out?
x = 0
for value in [3, 41, 12, 9, 74, 15] :
if value < 10 :
x = x + value
print x
41
12
15
9
"""
#Answer: 12
"""
Question 17
What would the following Python code print out?
fline = "blah blah"
if len(fline) > 1 :
print "More than one"
if fline[0] == "h" :
print "Has an h"
print "All done"
More than one
All done
More than one
Has an h
All done
Has an h
All done
Nothing will print
"""
#Answer: More than one, all done
"""
Question 18
What is the value of the following expression:
abc = 1 - 2 + 3 * 4 - 5 - 6 / 3
4
18
42
0
"""
# Answer: 4
"""
Question 19
What would the following Python code print out?
stx = "hello there bob how are you"
wds = stx.split()
print wds[2]
bob
are
e
how
"""
# Answer: bob
"""
Question 20
For the following Python program, what will it print out?
x = -1
for value in [3, 41, 12, 9, 74, 15] :
if value < x :
x = value
print x
21
74
15
-1
"""
# Answer: -1