Python Loop MCQ : Set 3

Python Loop MCQ

1). What is the output of the following code?

for i in range(5):
    if i == 2:
        break
    print(i)
else:
    print(“Loop completed”)

a) 0 1
b) 0 1 2
c) 0 1 2 3 4
d) Loop completed

Correct answer is: a) 0 1
Explanation: The for loop iterates from 0 to 4. When i becomes 2, the break statement is executed, terminating the loop prematurely.

2). Which loop control statement is used to terminate the entire program execution in Python?
a) stop
b) exit
c) terminate
d) quit

Correct answer is: b) exit
Explanation: The exit() function is used to terminate the entire program execution, not just loops.

3). What is the output of the following code?

i = 1
while i < 5:
    print(i)
    if i == 3:
        exit()
    i += 1

a) 1 2
b) 1 2 3 4
c) 1 2 3
d) The program terminates prematurely.

Correct answer is: d) The program terminates prematurely.
Explanation: The exit() function is used to terminate the entire program execution, not just loops. In this case, the program exits when i becomes 3.

4). What is the output of the following code?

for i in range(3):
    for j in range(2):
        print(i, j)
    else:
        print(“Inner loop completed”)
else:
print(“Outer loop completed”)

a) 0 0
0 1
1 0
1 1
2 0
2 1
Inner loop completed
Inner loop completed
Inner loop completed
Outer loop completed
b) 0 0
0 1
1 0
1 1
2 0
2 1
Inner loop completed
Inner loop completed
Inner loop completed
c) 0 0
0 1
1 0
1 1
Inner loop completed
2 0
2 1
Inner loop completed
Inner loop completed
Outer loop completed
d) 0 0
0 1
1 0
1 1
2 0
2 1
Inner loop completed
Inner loop completed
Outer loop completed

Correct answer is: a) 0 0
0 1
1 0
1 1
2 0
2 1
Inner loop completed
Inner loop completed
Inner loop completed
Outer loop completed
Explanation: The inner loop executes normally for each iteration of the outer loop. The else block after the inner loop is executed if the inner loop completes all its iterations without encountering a break statement. The outer loop’s else block is executed after the outer loop finishes all its iterations without encountering a break statement.

5). What is the output of the following code?

i = 1
while i <= 3:
     print(i)
     i += 1
else:
    print(“Loop completed”)

a) 1 2 3
Loop completed
b) 1 2 3 4
Loop completed
c) 2 3 4
Loop completed
d) Loop completed

Correct answer is: a) 1 2 3
Loop completed
Explanation: The while loop prints the value of i and increments it until i becomes greater than 3. After completing all iterations, the else block is executed and prints “Loop completed”.

6). What is the output of the following code?

i = 1
while i <= 3:
    print(i)
    i += 1
    if i == 2:
        continue
else:
print(“Loop completed”)

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, when executed, skips the remaining code for the current iteration and moves to the next iteration. In this case, it does not have any effect as it is placed after the increment statement.

7). What is the output of the following code?

for i in range(3):
    if i == 2:
        exit()
    print(i)
else:
   print(“Loop completed”)

a) 0 1
b) 0 1 2
c) 0 1 2 3
d) The program terminates prematurely.

Correct answer is: d) The program terminates prematurely.
Explanation: The exit() function is used to terminate the entire program execution, not just loops. In this case, the program exits when i become 2.

8). What is the output of the following code?

for i in range(1,50):
    if i%7 == 0 and i%5 == 0:
        print(i, end=” “)

a) 28
b) 25
c) 35
d) 49

Correct answer is: c)35
Explanation: The code iterates over the range of numbers from 1 to 49 (excluding 50). For each number, it checks if the number is divisible by both 7 and 5. If the condition is true, the number is printed

9). 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 if condition checks if i is divisible by 2 (i.e., even). When i becomes 2, the break statement is executed, terminating the loop after the first iteration.

10). What is the output of the following code?

i = 1
while i <= 5:
    print(i)
    i += 2

a) 1 2 3
b) 1 3 5
c) 2 4 6
d) 1

Correct answer is: b) 1 3 5
Explanation: The while loop prints the value of i and increments it by 2 until i becomes greater than 5.

11). What is the output of the following code?

for i in range(5):
    if i == 3:
        continue
    print(i)

a) 0 1 2 4
b) 0 1 2 3 4
c) 1 2 3 4
d) 0 1 2

Correct answer is: a) 0 1 2 4
Explanation: When i is equal to 3, the continue statement is executed, skipping the remaining code for that iteration.

12). What is the output of the following code?

for i in range(0,11):
    if i != 3 or i != 6:
        print(i,end=” “)

a) 0 1 2 3 4 5 6 7 8 9 10
b) 0 1 2 4 5 6 7 8 9 10
c) 0 1 2 3 4 5 7 8 9 10
d) 0 1 2 3 4 5 6 7 8 9

Correct answer is: b) 0 1 2 4 5 6 7 8 9 10
Explanation: The condition i != 3 or i != 6 is always true for any value of i. This is because if i is 3, then it is not equal to 6, and vice versa. As a result, the condition does not exclude any values of i. Therefore, the code will print all the numbers from 0 to 10, except 3 and 6.

13). What is the output of the following code?

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

a) 1 2 3
b) 1 2 3 4
c) 1 2 3 4 5
d) 3

Correct answer is: a) 1 2 3
Explanation: The while loop executes until i is less than 5. When i becomes 3, the break statement is executed, terminating the loop prematurely.

14). What is the purpose of the else block in a loop?
a) It is executed if the loop encounters an error.
b) It defines the initial condition of the loop.
c) It specifies the code to execute after the loop finishes normally.
d) It breaks the loop prematurely.

Correct answer is: c) It specifies the code to execute after the loop finishes normally.
Explanation: The else block in a loop is executed when the loop completes all its iterations without encountering a break statement.

15). What is the output of the following code?

for i in range(3):
    print(i)
else:
    print(“Loop completed”)

a) 0 1 2
Loop completed
b) 1 2 3
Loop completed
c) 0 1 2 3
Loop completed
d) 1 2 3 4
Loop completed

Correct answer is: a) 0 1 2
Loop completed
Explanation: The for loop iterates from 0 to 2, and after completing all iterations, the else block is executed and prints “Loop completed”.

16). What is the output of the following code?

for i in range(1, 6):
    if i % 2 == 0:
        print(i)

a) 2 4
b) 1 3 5
c) 1 2 3 4 5
d) 1 2 3 4

Correct answer is: a) 2 4
Explanation: The if condition checks if i is divisible by 2 (i.e., even). If it is even, the print statement is executed, displaying the value of i.

17). What is the output of the following code?

num1 = 0
num2 = 1
count = 0

while count < 10:
    print(num1,end=” “)
    n2 = num1 + num2
    num1 = num2
    num2 = n2
    count += 1

a) 0 1 1 2 3 5 8 13 21 34
b) 0 1 2 3 4 5 6 7 8 9
c) 1 2 3 4 5 6 7 8 9 10
d) 0 1 1 3 4 7 11 18 29 47

Correct answer is: a) 0 1 1 2 3 5 8 13 21 34
Explanation: The given code implements the Fibonacci sequence using a while loop. In each iteration, it prints the value of num1 and calculates the next Fibonacci number by updating the values of num1 and num2. The loop continues for 10 iterations, printing the Fibonacci sequence: 0 1 1 2 3 5 8 13 21 34.

18). 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) 2 4 5
d) 1 2 3 5

Correct answer is: a) 1 2 4 5
Explanation: When i is equal to 3, the continue statement is executed, skipping the remaining code for that iteration.

19). What is the output of the following code?

i = 1
while i <= 5:
    print(i)
    i += 2

a) 1 2 3 4 5
b) 1 3 5
c) 2 4 6
d) 1

Correct answer is: b) 1 3 5
Explanation: The while loop prints the value of i and increments it by 2 until i becomes greater than 5.

20). 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, when executed, skips the remaining code for the current iteration and moves to the next iteration. In this case, it does not have any effect as it is placed after the increment statement.

21). What is the output of the following code?
    
    for i in range(5):
        if i == 2:
            break
        print(i)
    else:
        print(“Loop completed”)
    
    a) 0 1
    b) 0 1 2
    c) 0 1 2 3 4
    d) Loop completed
 
Correct answer is: a) 0 1
Explanation: The for loop iterates from 0 to 4. When i becomes 2, the break statement is executed, terminating the loop prematurely.
 
22). Which loop control statement is used to terminate the entire program execution in Python?
    a) stop
    b) exit
    c) terminate
    d) quit
 
    Correct answer is: b) exit
    Explanation: The exit() function is used to terminate the entire program execution, not just loops.
 
23). What is the output of the following code?
    
    i = 1
    while i < 5:
        print(i)
        if i == 3:
            exit()
        i += 1
    
    a) 1 2
    b) 1 2 3 4
    c) 1 2 3
    d) The program terminates prematurely.
 
Correct answer is: d) The program terminates    prematurely.
Explanation: The exit() function is used to terminate the entire program execution, not just loops. In this case, the program exits when i becomes 3.
 
24). What is the output of the following code?
    
    for i in range(3):
        for j in range(2):
            print(i, j)
    
    a) 0 0
       0 1
       1 0
       1 1
       2 0
       2 1
    b) 0 0
       0 1
       1 0
       1 1
       2 0
    c) 0 0
       0 1
       1 0
       1 1
    d) 0 0
       0 1
 
Correct answer is: a) 0 0
       0 1
       1 0
       1 1
       2 0
       2 1
Explanation: The outer loop iterates three times, and for each iteration, the inner loop iterates twice, resulting in a total of six outputs.
 
25). What is the output of the following code?
    
    for i in range(3):
        print(i)
    else:
        print(“Loop completed”)
    
    a) 0 1 2
       Loop completed
    b) 1 2 3
       Loop completed
    c) 0 1 2 3
       Loop completed
    d) 1 2 3 4
       Loop completed
 
Correct answer is: a) 0 1 2
       Loop completed
Explanation: The for loop iterates from 0 to 2, and after completing all iterations, the else block is executed and prints “Loop completed”.

Leave a Comment