-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathChapter 3_Python quiz.py
More file actions
197 lines (114 loc) · 4.44 KB
/
Copy pathChapter 3_Python quiz.py
File metadata and controls
197 lines (114 loc) · 4.44 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
1.Question 1
What do we do to a Python statement that is immediately after an if statement to indicate that the statement is to be executed only when the if statement is true?
<A> Indent the line below the if statement
<B> Begin the statement with a curly brace {
<C> Underline all of the conditional code
<D> Start the statement with a "#" character
AnS: <A> Indent the line below the if statement
2.Question 2
Which of these operators is not a comparison / logical operator?
<A> ==
<B> =
<C> >=
<D> <=
<E> !=
AnS: <B> =
3.Question 3
What is true about the following code segment:
f x == 5 :
print('Is 5')
print('Is Still 5')
print('Third 5')
<A> Depending on the value of x, either all three of the print statements will execute or none of the statements will execute
<B> The string 'Is 5' will always print out regardless of the value for x.
<C> The string 'Is 5' will never print out regardless of the value for x.
<D> Only two of the three print statements will print out if the value of x is less than zero.
AnS: <A> Depending on the value of x, either all three of the print statements will execute or none of the statements will execute
4.Question 4
When you have multiple lines in an if block, how do you indicate the end of the if block?
<A> You put the colon : character on a line by itself to indicate we are done with the if block
<B> You use a curly brace { after the last line of the if block
<C> You omit the semicolon ; on the last line of the if block
<D> You de-indent the next line past the if block to the same level of indent as the original if statement
Ans: <D> Depending on the value of x, either all three of the print statements will execute or none of the statements will execute
5.Question 5
You look at the following text:
if x == 6 :
print('Is 6')
print('Is Still 6')
print('Third 6')
It looks perfect but Python is giving you an 'Indentation Error' on the second print statement. What is the most likely reason?
<A> Python thinks 'Still' is a mis-spelled word in the string
<B> In order to make humans feel inadequate, Python randomly emits 'Indentation Errors' on perfectly good code - after about an hour the error will just go away without any changes to your program
<C> Python has reached its limit on the largest Python program that can be run
<D> You have mixed tabs and spaces in the file
AnS: <D> You have mixed tabs and spaces in the file
6.Question 6
What is the Python reserved word that we use in two-way if tests to indicate the block of code that is to be executed if the logical test is false?
<A> iterate
<B> otherwise
<C> A closing curly brace followed by an open curly brace like this }{
<D> else
AnS: <D> else
7.Question 7
What will the following code print out?
x = 0
if x < 2 :
print('Small')
elif x < 10 :
print('Medium')
else :
print('LARGE')
print('All done')
<A> Medium
All done
<B> Small
<C> Small
Medium
LARGE
All done
<D> Small
All done
AnS: <D> Small
All done
8.Question 8
For the following code,
if x < 2 :
print('Below 2')
elif x >= 2 :
print('Two or more')
else :
print('Something else')
What value of 'x' will cause 'Something else' to print out?
<A> x = -2
<B> x = 2
<C> x = 2.0
<D> This code will never print 'Something else' regardless of the value for 'x'
AnS: <D> This code will never print 'Something else' regardless of the value for 'x'
9.Question 9
In the following code (numbers added) - which will be the last line to execute successfully?
(1) astr = 'Hello Bob'
(2) istr = int(astr)
(3) print('First', istr)
(4) astr = '123'
(5) istr = int(astr)
(6) print('Second', istr)
<A> 1
<B> 2
<C> 5
<D> 6
AnS: <A> 1
10.Question 10
For the following code:
astr = 'Hello Bob'
istr = 0
try:
istr = int(astr)
except:
istr = -1
What will the value be for istr after this code executes?
<A> false
<B> It will be the 'Not a number' value (i.e. NaN)
<C> -1
<D> It will be a random number depending on the operating system the program runs on
AnS: <C> -1