Python Loop MCQ : Set 2

Python Loop MCQ

1). Which loop control statement is used to terminate the loop prematurely and transfer control to the next iteration?
a) continue
b) exit
c) break
d) pass

Correct answer is: c) break
Explanation: The break statement is used to terminate a loop prematurely and transfer control to the next statement after the loop.

2). 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.

3). Which loop control statement is used to skip the remaining code in the current iteration and move to the next iteration?
a) continue
b) exit
c) break
d) pass

Correct answer is: a) continue
Explanation: The continue statement is used to skip the remaining code in the current iteration and move to the next iteration.

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

				
					for i in range(1, 6):
        if i == 3:
            continue
        print(i)
				
			

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

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.

5). What is the purpose of the pass statement in Python loops?
a) It terminates the loop.
b) It defines the initial condition of the loop.
c) It specifies the code to execute after the loop finishes normally.
d) It acts as a placeholder for future code.

Correct answer is: d) It acts as a placeholder for future code.
Explanation: The pass statement is a null operation in Python. It is used as a placeholder when a statement is syntactically required but no action is needed.

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

				
					    for i in range(3):
        pass
    print("Loop completed")
				
			

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

Correct answer is: a) Loop completed
Explanation: The pass statement has no effect on the loop execution in Python. It is used as a placeholder. After the loop is completed, “Loop completed” is printed.

7). Which loop control statement is used to terminate the entire loop structure in Python?

a) continue
b) exit
c) break
d) pass

Correct answer is: c) break
Explanation: The break statement terminates the innermost loop structure it is inside. If used in a nested loop, it terminates the inner loop.

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

				
					for i in range(3):
        for j in range(3):
            if j == 2:
                break
            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
1 2
2 0
2 1
d) 0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2

Correct answer is: b) 0 0
0 1
1 0
1 1
2 0
Explanation: The inner loop terminates prematurely when j equals 2 due to the break statement. The outer loop continues normally.

9). Which loop control statement is used to terminate the entire loop structure and move to the next iteration of the outer loop in nested loops?
a) continue
b) exit
c) break
d) pass

Correct answer is: c) break
Explanation: The break statement, when used in a nested loop, terminates the innermost loop structure and moves to the next iteration of the outer loop.

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

				
					for i in range(3):
        for j in range(3):
            if j == 2:
                continue
            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
1 2
2 0
2 1
d) 0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2

Correct answer is: a) 0 0
0 1
1 0
1 1
2 0
Explanation: The continue statement skips the remaining parts of the inner loop for that iteration when j equals 2. It moves to the next iteration of the inner loop.

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

				
					    for i in range(3):
        for j in range(3):
            if j == 2:
                break
            print(i, j)
            else:
                print("Inner loop completed")
    else:
        print("Outer loop completed")
				
			

a) 0 0
0 1
Inner loop completed
1 0
1 1
Inner loop completed
2 0
2 1
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
Outer 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
Inner loop completed
1 0
1 1
Inner loop completed
2 0
2 1
Outer loop completed

Correct answer is:
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
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.

12). Which loop control statement is used to terminate the entire loop structure and stop further iterations?
a) continue
b) exit
c) break
d) pass

Correct answer is: b) exit
Explanation: The exit statement is not a built-in loop control statement in Python. It is not used to terminate loops instead, it is typically used to terminate the entire program execution.

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

				
					for i in range(3):
        for j in range(3):
            if j == 2:
                exit()
            print(i, j)
				
			

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

Correct answer is: d) The program terminates prematurely.
Explanation: The exit() function is not a built-in loop control statement in Python. It is typically used to terminate the entire program execution, not just loops.

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

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

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 not a built-in loop control statement in Python. It is typically used to terminate the entire program execution, not just loops. In this case, the program exits when i becomes 3.

15). Which loop control statement is used to restart the current iteration from the beginning of the loop?
a) continue
b) restart
c) break
d) pass

Correct answer is: a) continue
Explanation: The continue statement is used to skip the remaining code for the current iteration and move to the next iteration from the beginning of the loop.

16). 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.

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

				
					numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9)
even = 0
odd = 0

for val in numbers:
   	if val%2 == 0:
        	even += 1
    	else:
        	odd += 1
				
			

print(“Number of even numbers: “,even)
print(“Number of odd numbers: “,odd)

a) Number of even numbers: 4, Number of odd numbers: 5
b) Number of even numbers: 5, Number of odd numbers: 4
c) Number of even numbers: 0, Number of odd numbers: 0
d) Number of even numbers: 9, Number of odd numbers: 0

Correct answer is: a) Number of even numbers: 4, Number of odd numbers: 5
Explanation: The given code iterates over the numbers tuple and counts the number of even and odd numbers. In the provided tuple, there are 4 even numbers (2, 4, 6, 8) and 5 odd numbers (1, 3, 5, 7, 9). The final output will display the count of even and odd numbers accordingly.

18). 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.

19). 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.

20). 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”.

21). 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.

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

				
					    num = 1231254315
    str1 = str(num)
    D1 = {}
    for val in str1:
    	D1[val] = str1.count(val)
 
    print(D1)
				
			

a) {‘1’: 3, ‘2’: 2, ‘3’: 2, ‘4’: 1, ‘5’: 2}
b) {‘1’: 1, ‘2’: 1, ‘3’: 1, ‘4’: 1, ‘5’: 1}
c) {‘1’: 1, ‘2’: 2, ‘3’: 1, ‘4’: 2, ‘5’: 1}
d) {‘1’: 2, ‘2’: 1, ‘3’: 2, ‘4’: 1, ‘5’: 1}

Correct answer is: a) {‘1’: 3, ‘2’: 2, ‘3’: 2, ‘4’: 1, ‘5’: 2}
Explanation: The code counts the occurrence of each digit in the variable num and stores the counts in a dictionary D1. The final dictionary D1 contains the counts of digits in the original number num. The correct counts are: ‘1’: 2, ‘2’: 2, ‘3’: 2, ‘4’: 1, ‘5’: 1.

23). 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.

24). 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.

25). 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.

Leave a Comment