0% found this document useful (0 votes)
28 views6 pages

Module 5 Python Control Statements

Module 5 covers Python control statements including if, if-else, while, and for statements. It includes a test section with multiple-choice questions assessing understanding of control structures and their outputs. The document provides code snippets and options for debugging and completing code related to decision structures and loops.

Uploaded by

SHUBHANGEE JHA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views6 pages

Module 5 Python Control Statements

Module 5 covers Python control statements including if, if-else, while, and for statements. It includes a test section with multiple-choice questions assessing understanding of control structures and their outputs. The document provides code snippets and options for debugging and completing code related to decision structures and loops.

Uploaded by

SHUBHANGEE JHA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Module 5: Python Control Statements

Python provides the following options:


❑ if statement
❑ if else statement
❑ if elif else statement
❑ while statement
❑ for statement

Module 5: Python Control Statements Test:

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:

print("Income is greater than 3000")

elif income > 4000:

print("Income is greater than 4000")

Options:
A. Income is greater than 3000

B. Income is greater than 3000 followed by Income is greater than 4000

C. Income is greater than 4000

D. Income is greater than 4000 followed by 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:

Percentage range Letter grade


90 through 100 A
80 through 89 B
70 through 79 C
65 through 69 D
0 through 64 F
For example, if the user enters a 90, the output should be, “Your letter grade is
A.” Likewise, if a user enters an 89, the output should be “Your letter grade is
B.”
How should you complete the code? To answer, select the appropriate code
segments in the answer area.

#Letter Grade converter


grade = int(input(”Enter a numeric grade’))
[Option1]
letter_grade = ‘A’
[Option2] _
letter_grade = ‘B’
[Option3] _
letter_grade = ‘C’
[Option4] _
letter_grade = ‘D’
else:
letter_grade = ‘F’
print(”Your letter grade is :“, letter_grade)

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.

num = int(input(“Enter a number with 1 or 2 digits: “))


digits = 0;
[Option1]
digits = “1”
[Option2]
digits = 2
[Option3]
digits = “>2”
print(digits + “ digits.”)
Options:

[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:

Q7) What will be displayed when the following code is executed?


number = 6
while number > 0:
number -= 3
print(number, end = ' ')

Options:
A. 6 3 0 B. 6 3
C. 3 0 D. 3 0 -3
Solution:

Q8) What is the output of the following?


i = 1
while True:
if i%2 == 0:
break
print(i, end = ' ')
i += 2

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:

Q10) What is the output of the following?


for i in range(2.0):
print(i)
Options:
a) 0.0 1.0
b) 0 1
c) error
d) none of the mentioned
Solution:

M 5-5
Q11) The following loop displays .

for i in range(1, 11):


print(i, end = " ")
Options:
A. 1 2 3 4 5 6 7 8 9
B. 1 2 3 4 5 6 7 8 9 10
C. 1 2 3 4 5
D. 1 3 5 7 9
Solution:

Q12) What is the output for y?


y = 0
for i in range(10, 1, -2):
y += i
print(y)
Options:
A. 10
B. 40
C. 30
D. 20
Solution:

Q13) What is the output of the following?

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

You might also like