Python MCQ on Loops

Loops are essential in any programming language, allowing for repetitive tasks to be carried out efficiently. In Python, the primary loop constructs are the <em>for</em> and <em>while</em> loops. Check your understanding of Python loops with these 10+ Multiple Choice Questions.

1. Which loop is used to iterate over a sequence (like a list or a string)?

a) do-while
b) repeat-until
c) for
d) loop

Answer:

c) for

Explanation:

The for loop in Python is used to iterate over a sequence.

2. How would you loop through numbers from 1 to 5 in Python?

a) for i in range(1, 5)
b) for i = 1 to 5
c) for i in range(1, 6)
d) for i = 1; i <= 5; i++

Answer:

c) for i in range(1, 6)

Explanation:

In Python, range(1, 6) generates numbers from 1 to 5. The ending number is exclusive in the range function.

3. Which keyword is used to exit out of a loop prematurely?

a) stop
b) end
c) break
d) exit

Answer:

c) break

Explanation:

The break keyword is used to exit a loop before it has finished iterating.

4. Which keyword will skip the current iteration and continue with the next one?

a) skip
b) jump
c) continue
d) pass

Answer:

c) continue

Explanation:

The continue keyword is used to skip the current iteration of a loop and proceed to the next one.

5. What is the output of the following code?

for i in range(3):
    print(i)
a) 0 1 2 3
b) 1 2 3
c) 0 1 2
d) Error

Answer:

c) 0 1 2

Explanation:

By default, range(n) starts from 0 and goes up to, but does not include, n.

6. How would you loop over a string one character at a time?

a) for char in len(string):
b) for char from string:
c) for char in string:
d) for char of string:

Answer:

c) for char in string:

Explanation:

Strings are sequences in Python, and each character of a string can be accessed using a for loop directly.

7. Which of the following results in an infinite loop?

a) while False:
b) while True:
c) for i in range(5):
d) for i in “Python”:

Answer:

b) while True:

Explanation:

while True: will always evaluate as true, causing the loop to run indefinitely.

8. What does the else clause do in a Python loop?

a) Executes if the loop condition was never True.
b) Executes if the loop didn’t encounter a break statement.
c) Executes if the loop encountered a continue statement.
d) Executes each time the loop iterates.

Answer:

b) Executes if the loop didn’t encounter a break statement.

Explanation:

The else clause in a Python loop is executed when the loop finishes normally (i.e., without encountering a break).

9. Which loop is typically used when you know beforehand how many times you want to iterate?

a) do-while
b) for
c) while
d) until

Answer:

b) for

Explanation:

The for loop is typically used when you know the number of iterations beforehand, often determined by the length of a sequence or the parameters of the range() function.

10. Which function would you use to get numbers in descending order, starting from 5 and ending at 1?

a) range(5, 0)
b) range(5, 0, -1)
c) range(1, 5, -1)
d) range(5, 1)

Answer:

b) range(5, 0, -1)

Explanation:

range(5, 0, -1) starts at 5 and decrements the number by 1 until reaching 0 (exclusive).

11. Which statement is used to represent a block of code/body of a loop that does nothing?

a) skip
b) none
c) null
d) pass

Answer:

d) pass

Explanation:

The pass statement is a null operation; nothing happens when it executes. It is useful as a placeholder where a statement is syntactically needed but no action is required.

12. In a nested loop, what does the break statement do if it is encountered?

a) It exits all loops.
b) It exits only the innermost loop.
c) It exits only the outermost loop.
d) It pauses the execution of the innermost loop.

Answer:

b) It exits only the innermost loop.

Explanation:

When a break statement is encountered inside a nested loop, only the innermost loop in which the break exists is exited.


Looping is fundamental in programming. I hope these questions enhanced your understanding of loops in Python. Keep practicing and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top