Module 5 Python Control Statements
Module 5 Python Control Statements
Q1)
even = False
if even = True:
print("It is even!")
Options:
A. The program has a syntax error in line 1 (even = False)
B. The program has a syntax error in line 2 if even = True is not a correct
condition. It should be replaced by if even == True: or if even:.
C. The program runs, but displays nothing.
D. The program runs and displays It is even!.
Solution:
Q2) Suppose income is 4001, what will be displayed by f the following code?
if income > 3000:
Options:
A. Income is greater than 3000
Solution:
M 5-1
Q3) You are designing a decision structure to convert a student’s numeric grade to a
letter grade. The program must assign a letter grade as specified in the following
table:
Options:
Option1: Option2:
A. if grade <= 90: B. if grade >= 90: A. if grade > 80: B. if grade >= 80:
C. elif grade > 90: D. elif grade >= 90: C. elif grade > 80: D. elif grade >= 80:
Option3: Option4:
A. if grade > 70: B. if grade >= 70: A. if grade > 65: B. if grade >= 65:
C. elif grade > 70: D. elif grade >= 70: C. elif grade > 65: D. elif grade >= 65:
Solution:
M 5-2
Q4) What is the output of the code shown below?
if (9 < 0) and (0 < -9):
print("hello")
elif (9 > 0) or False:
print("good")
else:
print("bad")
Options:
a) error b) hello
c) good d) bad
Solution:
Q5) You are writing a Python Practice Test program to ask the user to enter a number
and determine if the number is 1 digit 2 digits, or more than 2 digits long. You need to
write the program. How should you complete the code? To answer, select the
appropriate code segments in the answer area.
[Option1]
A. if num > -10 and num < 10: B. if num > -100 and num < 100:
[Option2]
A. if num > -100 and num < 100: B. elif num > -100 and num < 100:
C. if num > -10 and num < 10: D. elif num > -10 and num < 10:
[Option3]
A. else: B. elif:
Solution:
M 5-3
Q6) How many times will the following code print "Welcome to Python"?
count = 0
while count < 10:
print("Welcome to Python")
Options:
A. 9
B. 10
C. 11
D. infinite number of times
Solution:
Options:
A. 6 3 0 B. 6 3
C. 3 0 D. 3 0 -3
Solution:
Options:
a) 1 b) 1 2
c) 1 2 3 4 5 6 … d) 1 3 5 7 9 11 …
Solution:
M 5-4
Q9) A classmate has asked you to debug the following code:
x = 4
while x >= 1:
if x % 4 == 0:
print ("party“, end=‘ ’)
elif x - 2 < 0:
print("cake", end=‘ ’)
elif x / 3 == 0:
print("greeting", end=‘ ’)
else:
print("birthday", end=‘ ’)
x = x - 1
What is the output that is printed to the screen?
Options:
A. birthday party greeting cake
B. party birthday birthday cake
C. party greeting birthday cake
D. birthday greeting party cake
Solution:
M 5-5
Q11) The following loop displays .
x = 'abcd'
for i in range(len(x)):
print(i, end = ' ')
Options:
a) a b c d
b) 0 1 2 3
c) error
d) 1 2 3 4
Solution:
M 5-6