Python Loop MCQ
1). What is the output of the following code?
for i in range(5):
if i % 2 == 0:
continue
print(i)
a) 1 3
b) 0 2 4
c) 1 2 3 4
d) 0 1 2 3 4
Correct answer is: a) 1 3
Explanation: The if condition checks if i is divisible by 2 (i.e., odd). If it is odd, the continue statement is executed, skipping the remaining code for that iteration.
2). What is the output of the following code?
i = 1
while i <= 5:
print(i)
i += 1
else:
print(“Loop completed”)
a) 1 2 3 4 5
Loop completed
b) 1 2 3 4 5 6
Loop completed
c) 2 3 4 5 6
Loop completed
d) 1 2 3 4 5 6 7
Loop completed
Correct answer is: a) 1 2 3 4 5
Loop completed
Explanation: The while loop prints the value of i and increments it until i becomes greater than 5. After completing all iterations, the else block is executed and prints “Loop completed”.
3). What is the output of the following code?
for i in range(1, 6):
if i == 3:
break
print(i)
a) 1 2
b) 1 2 3
c) 1 2 3 4 5
d) 2 3 4 5
Correct answer is: a) 1 2
Explanation: The for loop iterates from 1 to 5. When i becomes 3, the break statement is executed, terminating the loop prematurely.
4). What is the output of the following code?
i = 0
while i < 3:
print(i)
i += 1
if i == 2:
break
a) 0 1
b) 0 1 2
c) 0 1 2 3
d) 0
Correct answer is: a) 0 1
Explanation: When i is equal to 2, the break statement is executed, terminating the loop prematurely.
5). What is the output of the following code?
i = 0
while i < 5:
i += 1
if i == 3:
continue
print(i)
a) 1 2 4 5
b) 1 2 3 4 5
c) 1 3 4 5
d) 2 3 4 5
Correct answer is: c) 1 3 4 5
Explanation: When i is equal to 3, the continue statement is executed, skipping the remaining code for that iteration.
6). What is the output of the following code?
numbers = [1, 2, 3, 4, 5]
for number in numbers:
if number % 2 == 0:
continue
print(number)
a) 1 2 3 4 5
b) 1 3 5
c) 2 4
d) 1
Correct answer is: b) 1 3 5
Explanation: The code iterates over the list `numbers`. When a number is even, the `continue` statement is executed, skipping the remaining code for that iteration.
7). Which loop in Python allows the execution of a block of code for a fixed number of times?
a) while loop
b) for loop
c) do-while loop
d) until loop
Correct answer is: b) for loop
Explanation: The `for` loop in Python allows the execution of a block of code for a fixed number of times, iterating over a sequence or collection.
8). What is the output of the following code?
for i in range(5, 0, -1):
print(i)
a) 5 4 3 2 1
b) 1 2 3 4 5
c) 0 1 2 3 4
d) 4 3 2 1 0
Correct answer is: a) 5 4 3 2 1
Explanation: The `range()` function starts from 5 and decrements by 1 until it reaches 1 (excluding 0). The numbers are then printed in reverse order.
9). What is the output of the following code?
i = 0
while i < 5:
if i == 3:
break
print(i)
i += 1
a) 0 1 2 3 4
b) 0 1 2
c) 0 1 2 3
d) 0
Correct answer is: b) 0 1 2
Explanation: The `break` statement is executed when `i` becomes 3, terminating the loop prematurely.
10). What is the output of the following code?
numbers = [1, 2, 3, 4, 5]
for index in range(len(numbers)):
if numbers[index] % 2 == 0:
break
print(numbers[index])
a) 1 2 3 4 5
b) 1 3 5
c) 2 4
d) 1
Correct answer is: d) 1
Explanation: The code iterates over the indices of the `numbers` list. When an even number is encountered, the `break` statement is executed, terminating the loop.
11). What is the output of the following code?
i = 1
while True:
print(i)
i += 1
if i == 5:
break
a) 1 2 3 4
b) 1 2 3 4 5
c) 1 2 3 4 5
d) The program enters an infinite loop.
Correct answer is: a) 1 2 3 4
Explanation: The `while` loop continues indefinitely until the ‘break’ statement is executed when `i` becomes 5.
12). What is the output of the following code?
for i in range(1, 6):
if i % 2 == 0:
break
print(i)
a) 1
b) 1 2
c) 1 3 5
d) 1 2 3 4 5
Correct answer is: a) 1
Explanation: The `break` statement is executed when an even number is encountered, terminating the loop after the first iteration.
13). What is the output of the following code?
i = 0
while i < 5:
i += 1
if i % 2 == 0:
continue
print(i)
a) 1 2 3 4 5
b) 1 3 5
c) 2 4
d) 1 2 4 5
Correct answer is: b) 1 3 5
Explanation: The `continue` statement is executed when an even number is encountered, skipping the remaining code for that iteration.
14). What is the output of the following code?
for i in range(5):
print(i)
a) 0 1 2 3 4
b) 0 1 2 3
c) 1 2 3 4 5
d) 1 2 3 4
Correct answer is: a) 0 1 2 3 4
Explanation: The `for` loop iterates from 0 to 4 (excluding 5), printing the value of `i` at each iteration.
15). What is the output of the following code?
i = 1
while i <= 3:
print(i)
i += 1
if i == 2:
continue
a) 1 2 3
b) 1 2
c) 1 2 3 4
d) 1
Correct answer is: a) 1 2 3
Explanation: The `continue` statement does not have any effect in this case as it is placed after the increment statement.
16). What is the output of the following code?
numbers = [1, 2, 3, 4, 5]
for number in numbers:
if number == 3:
break
print(number)
a) 1 2 3 4 5
b) 1 2
c) 1 2 3
d) 1 2 4 5
Correct answer is: b) 1 2
Explanation: The `break` statement is executed when the number 3 is encountered, terminating the loop prematurely.
17). What is the output of the following code?
i = 0
while i < 5:
print(i)
i += 1
if i == 3:
continue
a) 0 1 2 3 4
b) 0 1 3 4
c) 0 1 2 3 4 5
d) 0 1 2 4
Correct answer is: a) 0 1 2 3 4
Explanation: The `continue` statement is executed when `i` is equal to 3, skipping the remaining code for that iteration.
18). What is the output of the following code?
for i in range(3):
for j in range(i):
print(i, j)
a) No output
b) 0 0
1 0
1 1
2 0
2 1
c) 0 0
1 0
1 1
2 0
d) 0 0
1 0
1 1
Correct answer is: c) 0 0
1 0
1 1
2 0
Explanation: The outer loop iterates three times, and for each iteration, the inner loop iterates `i` times, where `i` represents the current value of the outer loop.
19). What is the output of the following code?
i = 0
while i < 3:
i += 1
print(i)
if i == 2:
break
a) 1 2
b) 1 2 3
c) 0 1 2
d) 0 1 2 3
Correct answer is: a) 1 2
Explanation: The `break` statement is executed when `i` becomes 2, terminating the loop prematurely.
20). What is the output of the following code?
for i in range(1, 6):
print(i)
else:
print(“Loop completed”)
a) 1 2 3 4 5
Loop completed
b) 1 2 3 4 5 6
Loop completed
c) 2 3 4 5 6
Loop completed
d) 1 2 3 4 5 6 7
Loop completed
Correct answer is: a) 1 2 3 4 5
Loop completed
Explanation: The `else` block is executed after the completion of all iterations of the `for` loop.
21). What is the output of the following code?
numbers = [1, 2, 3, 4, 5]
for number in numbers:
print(number)
if number == 3:
break
else:
print(“Loop completed”)
a) 1 2 3
Loop completed
b) 1 2 3 4 5
Loop completed
c) 1 2 3 4 5
d) 1 2 3 4
Correct answer is: a) 1 2 3
Loop completed
Explanation: The `break` statement is executed when the number 3 is encountered, terminating the loop prematurely. The `else` block is not executed in this case.
22). What is the output of the following code?
i = 0
while i < 5:
if i == 2:
i += 1
continue
print(i)
i += 1
a) 0 1 2 3 4
b) 0 1 2 3
c) 0 1 3 4
d) 0 1 2 4
Correct answer is: b) 0 1 2 3
Explanation: The `continue` statement is executed when `i` is equal to 2, skipping the remaining code for that iteration.
23). What is the output of the following code?
for i in range(3):
for j in range(3):
if i == j:
break
print(i, j)
a) No output
b) 0 1
1 2
c) 0 1
0 2
1 2
d) 0 1
0 2
1 1
1 2
Correct answer is: a) No output
Explanation: The `break` statement is executed when `i` is equal to `j`, causing the inner loop to terminate for each iteration of the outer loop.
24). What is the output of the following code?
numbers = [1, 2, 3, 4, 5]
for number in numbers:
if number == 3:
continue
print(number)
a) 1 2 3 4 5
b) 1 3 4 5
c) 1 2 4 5
d) 2 3 4 5
Correct answer is: c) 1 2 4 5
Explanation: The `continue` statement is executed when the number 3 is encountered, skipping the remaining code for that iteration.
25). What is the output of the following code?
numbers = [1, 2, 3, 4, 5]
for number in numbers:
print(number)
if number == 3:
break
else:
print(“Loop completed”)
a) 1 2 3
Loop completed
b) 1 2 3 4 5
Loop completed
c) 1 2 3 4 5
d) 1 2 3 4
Correct answer is: a) 1 2 3
Loop completed
Explanation: The `break` statement is executed when the number 3 is encountered, terminating the loop prematurely. The `else` block is not executed in this case.