NEW
Font size
WorksheetsPython Conditional Statements and Loop Control MCQs
Total questions: 25
Worksheet time: 13mins
What is the output of the following code? x = 5 if x > 3: print("A") if x > 4: print("B") else: print("C")
A
A B
B
A C
Which of the following statements is TRUE about Python if?
Indentation is mandatory
Semicolon is mandatory
Curly braces are mandatory
Parentheses are mandatory
What will be printed? if 0: print("True") else: print("False")
True
False
Error
None
What is the output? for i in range(1, 5, 2): print(i, end=" ")
1 2 3 4
1 3
1 3 5
Error
How many times will the loop execute? i = 1 while i <= 5: i += 2
Infinite
4
3
2
What is the output? for i in range(3): for j in range(i): print("*", end="") print()
(newline)* ** ***
(newline)* **
(newline)* ** (newline)
* ** ***
What will be printed? for i in range(5): if i == 3: break print(i, end=" ")
0 1 2
0 1 2 3
Error
1 2 3
What is the output? for i in range(5): if i == 3: continue print(i, end=" ")
1 2 4
0 1 2 3 4
0 1 2
0 1 2 4
pass statement is used to:
Exit loop
Skip iteration
Do nothing
Terminate program
What is the output? try: x = 10 / 0 except ZeroDivisionError: print("Error")
Exception
Error
Program terminates
0
Which block is executed irrespective of exception?
except
else
try
finally
What will be printed? try: print(1) raise ValueError except ValueError: print(2) finally: print(3)
Error
1 2
1 2 3
1 3
Which of the following raises an exception?
int("10")
int("A")
float("3.5")
str(10)
What is the output? try: print("A") except: print("B") else: print("C")
A
A C
B
Error
Infinite loop is created using:
while True:
for i in range(0)
while 1 == 0:
for i in []:
What will be printed? for i in range(3): print(i) if i == 1: break print(i + 1)
0 1 1
0 1 2
0 1 1 2
0 1
What is the output of the following code? x = 10 if x < 5: print("Low") elif x < 15: print("Medium") else: print("High")
Low
Medium
Error
High
What will be printed? try: print("Start") raise Exception except Exception: print("Caught") else: print("No Exception")
Start Caught
Caught
Start
Start Caught No Exception
What will be printed? for i in range(4): if i == 2: continue print(i, end=" ")
0 1 3
1 2 3
0 1 2 3
0 1 2
What is the output of the following code? x = 7 if x < 10: print("Less than 10") else: print("10 or more")
None
Error
10 or more
Less than 10
Which of the following will cause a syntax error?
print("Hello")
if x = 5:
for i in range(5):
while True:
What will be printed? for i in range(4): if i == 1: continue print(i, end=" ")
0 1 2 3
1 2 3
0 2 3
0 1 3
Which of the following is NOT a valid way to define a function in Python?
lambda x: x + 1
def my_function():
function my_function():
def my_function(param):
What will be printed? for i in range(3): for j in range(2): if j == 1: break print(i, j)
0 0 1 0 2 0
0 0 1 0 2 1
0 0 1 0 2 0 2 1
0 0 1 0 2 0 2 1
Which of the following will NOT raise an exception?
int("10")
list(10)
int("A")
float("3.5")
