Python Loop MCQ : Set 1

Python Loop MCQ

1). What is a loop in Python?
a) A data structure
b) A control flow statement
c) A mathematical operation
d) A file handling technique

Correct answer is: b) A control flow statement
Explanation: A loop is a control flow statement that allows executing a block of code repeatedly until a certain condition is met.

2). Which loop in Python allows executing a block of code a fixed number of times?
a) for loop
b) while loop
c) do-while loop
d) nested loop

Correct answer is: a) for loop
Explanation: The for loop in Python is used to iterate over a sequence or range a fixed number of times.

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

				
					  for i in range(5):
       print(i)
				
			

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

Correct answer is: a) 0 1 2 3 4
Explanation: The range function generates a sequence of numbers from 0 to 4 (exclusive), and the for loop prints each number in the sequence.

4). Which keyword is used to exit a loop prematurely in Python?
a) continue
b) exit
c) break
d) return

Correct answer is: c) break
Explanation: The break keyword is used to exit a loop prematurely, skipping the remaining iterations.

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

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

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

Correct answer is: a) 0 1 2 3 4
Explanation: The while loop executes as long as the condition (i < 5) is true. The variable i is incremented by 1 in each iteration of the loop.

6). Which loop in Python allows executing a block of code as long as a condition is true?
a) for loop
b) while loop
c) do-while loop
d) nested loop

Correct answer is: b) while loop
Explanation: The while loop repeatedly executes a block of code as long as a condition is true.

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

				
					   i = 5
   while i > 0:
       print(i)
       i -= 1
				
			

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

Correct answer is: a) 5 4 3 2 1
Explanation: The while loop executes as long as the condition (i > 0) is true. The variable i is decremented by 1 in each iteration.

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

Correct answer is: a) continue
Explanation: The continue statement skips the current iteration of a loop and moves to the next iteration.

9). 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: The continue statement skips the iteration when i equals 3, so only the numbers 1, 2, 4, and 5 are printed.

10). Which loop allows executing a block of code at least once, even if the condition is initially false?
a) for loop
b) while loop
c) do-while loop
d) nested loop

Correct answer is: c) do-while loop
Explanation: Python doesn’t have a built-in do-while loop. However, we can achieve a similar behavior using a while loop with a condition at the end.

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

				
					    i = 5
    while True:
        print(i)
        i -= 1
        if i == 0:
            break
				
			

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

Correct answer is: a) 5 4 3 2 1
Explanation: The while loop executes indefinitely since the condition is always true. The break statement is used to exit the loop when i reaches 0.

12). Which loop is used to iterate over the elements of a sequence, such as a list or a string, in Python?

a) for loop
b) while loop
c) do-while loop
d) nested loop

Correct answer is: a) for loop
Explanation: The for loop in Python is commonly used to iterate over the elements of a sequence, such as a list or a string.

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

				
					    my_list = ['apple', 'banana', 'cherry']
    for item in my_list:
        print(item)
				
			

a) apple banana cherry
b) 1 2 3
c) 0 1 2
d) TypeError: ‘int’ object is not iterable

Correct answer is: a) apple banana cherry
Explanation: The for loop iterates over each element in the list `my_list` and prints them.

14). Which loop is used when we need to execute a section of code multiple times, with each iteration based on the results of the previous iteration?

a) for loop
b) while loop
c) do-while loop
d) nested loop

Correct answer is: b) while loop
Explanation: The while loop is useful when we need to repeat a section of code multiple times, and the number of iterations depends on the results of the previous iteration.

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

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

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

Correct answer is: a) 1 3 5
Explanation: The while loop executes as long as the condition (i < 5) is true. The variable i is incremented by 2 in each iteration.

16). What is the purpose of the range() function in a for loop?
a) It generates a sequence of numbers.
b) It defines the number of iterations.
c) It iterates over the elements of a list.
d) It breaks the loop prematurely.

Correct answer is: a) It generates a sequence of numbers.
Explanation: The range() function generates a sequence of numbers that can be used to control the number of iterations in a for loop.

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

				
					for i in range(3, 9, 2):
        print(i)
    
				
			

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

Correct answer is: a) 3 5 7
Explanation: The range function generates a sequence of numbers starting from 3 (inclusive) and ending at 9 (exclusive) with a step size of 2. The for loop iterates over this sequence and prints each number.

18). Which loop allows nesting one loop inside another loop?
a) for loop
b) while loop
c) do-while loop
d) nested loop

Correct answer is: d) nested loop
Explanation: Nested loops refer to the technique of having one loop inside another loop.

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

				
					for i in range(1, 4):
        for j in range(1, 4):
            print(i * j)
				
			

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

Correct answer is: a) 1 2 3 2 4 6 3 6 9
Explanation: The outer loop iterates over the values 1, 2, and 3, while the inner loop iterates over the same values. The product of i and j is printed in each iteration.

20). Which loop in Python is best suited for iterating over a dictionary?
a) for loop
b) while loop
c) do-while loop
d) nested loop

Correct answer is: a) for loop
Explanation: The for loop is commonly used to iterate over the keys or values of a dictionary in Python.

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

				
					my_dict = {'grapes': 2, 'banana': 3, 'blue-berry': 4}
for key in my_dict:
    print(key)
				
			

a) grapes banana blue-berry
b) 1 2 3
c) 0 1 2
d) TypeError: ‘int’ object is not iterable

Correct answer is: a) grapes banana blue-berry
Explanation: The for loop iterates over the keys of the dictionary `my_dict` and prints them.

22). Which statement can be used to modify the flow of a loop, allowing the next iteration to skip some parts of the loop’s code block?
a) continue
b) exit
c) break
d) pass

Correct answer is: a) continue
Explanation: The continue statement allows skipping the remaining parts of the loop’s code block for the current iteration and moving to the next iteration.

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

				
					for i in range(8):
        if i % 2 == 0:
            continue
        print(i)
				
			

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

Correct answer is: a) 1 3 5 7
Explanation: The if condition checks if i is divisible by 2 (i.e., even). If it is even, the continue statement is executed, and the loop skips the remaining code for that iteration.

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

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

				
					    for i in range(5):
        print(i)
    else:
        print("Loop completed")
				
			

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

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

Leave a Comment