0% found this document useful (0 votes)
53 views

MCQ On Python Loop

These questions test knowledge of Python while loops and break/else statements. The questions cover basic while loop syntax and behavior, including when the else block is executed and how break affects loop execution. Correct answers and explanations are provided.

Uploaded by

Rishita Saha
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)
53 views

MCQ On Python Loop

These questions test knowledge of Python while loops and break/else statements. The questions cover basic while loop syntax and behavior, including when the else block is executed and how break affects loop execution. Correct answers and explanations are provided.

Uploaded by

Rishita Saha
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/ 3

1. What will be the output of the following Python code?

i = 1
while True:
if i%2 == 0:
break
print(i)
i += 2
a) 1
b) 1 2
c) 1 2 3 4 5 6 …
d) 1 3 5 7 9 11 …

Answer: d
Explanation: The loop does not terminate since i is never an even number.

2. What will be the output of the following Python code?

i = 2
while True:
if i%3 == 0:
break
print(i)
i += 2
a) 2 4 6 8 10 …
b) 2 4
c) 2 3
d) error

Answer: b
Explanation: The numbers 2 and 4 are printed. The next value of i is 6 which is
divisible by 3 and hence control exits the loop.

3. What will be the output of the following Python code?

i = 1
while False:
if i%2 == 0:
break
print(i)
i += 2
a) 1
b) 1 3 5 7 …
c) 1 2 3 4 …
d) none of the mentioned

Answer: d
Explanation: Control does not enter the loop because of False.
4. What will be the output of the following Python code?

True = False
while True:
print(True)
break
a) True
b) False
c) None
d) none of the mentioned

Answer: d
Explanation: SyntaxError, True is a keyword and it’s value cannot be changed.

5. What will be the output of the following Python code?

i = 0
while i < 5:
print(i)
i += 1
if i == 3:
break
else:
print(0)

a) 0 1 2 0
b) 0 1 2
c) error
d) none of the mentioned

Answer: b
Explanation: The else part is not executed if control breaks out of the loop.

6. What will be the output of the following Python code?

i = 0
while i < 3:
print(i)
i += 1
else:
print(0)
a) 0 1 2 3 0
b) 0 1 2 0
c) 0 1 2
d) error

Answer: b
Explanation: The else part is executed when the condition in the while statement is
false.
7. What will be the output of the following Python code?

x = "abcdef"
i = "a"
while i in x:
print(i, end = " ")
a) no output
b) i i i i i i …
c) a a a a a a …
d) a b c d e f

Answer: c
Explanation: As the value of i or x isn’t changing, the condition will always evaluate
to True.

You might also like