Python For Loop MCQ Questions and Answers

The for loop in Python is a control flow statement that is used to iterate over a sequence (such as a list, tuple, set, dictionary, or string) or other iterable objects. Here we present 12 multiple-choice questions to test your knowledge of Python For Loop. Each MCQ has the correct answer with an explanation. 

1. What is the basic structure of a for loop in Python?

a) for item in iterable:
b) for (item in iterable)
c) for each item in iterable:
d) for item <= iterable:

Answer:

a) for item in iterable:

Explanation:

In Python, a for loop iterates over items of an iterable using the syntax 'for item in iterable:'.

2. How do you iterate over a range of numbers from 0 to 4 in Python?

a) for i in range(0, 5):
b) for i in range(5):
c) for i in range(1, 5):
d) Both a and b

Answer:

d) Both a and b

Explanation:

Both range(5) and range(0, 5) produce a sequence of numbers from 0 to 4.

3. How can you loop through a dictionary and access both keys and values?

a) for key in my_dict:
b) for key, value in my_dict.items():
c) for key in my_dict.keys() and value in my_dict.values():
d) for key, value in my_dict:

Answer:

b) for key, value in my_dict.items():

Explanation:

The items() method returns key-value pairs, which can be unpacked in a for loop.

4. What does the 'break' statement do inside a for loop?

a) Pauses the loop execution
b) Skips the current iteration
c) Exits the loop prematurely
d) Restarts the loop

Answer:

c) Exits the loop prematurely

Explanation:

The 'break' statement is used to exit the for loop before it has iterated over all items.

5. What is the role of the 'continue' statement in a for loop?

a) To stop the loop
b) To skip the current iteration and continue with the next
c) To pause loop execution
d) To terminate the loop immediately

Answer:

b) To skip the current iteration and continue with the next

Explanation:

The 'continue' statement skips the rest of the code inside a loop for the current iteration.

6. How do you iterate over both the elements of a list and their indices?

a) for i, item in enumerate(my_list):
b) for i in range(len(my_list)):
c) for item in my_list:
d) Both a and b

Answer:

d) Both a and b

Explanation:

You can use enumerate(my_list) or loop over the indices using range(len(my_list)).

7. What is the correct syntax for iterating over a list using a for loop?

a) for item in list:
b) for item <= list:
c) for item = list[0] to list[-1]:
d) for each item in list:

Answer:

a) for item in list:

Explanation:

The syntax for iterating over a list is 'for item in list:'.

8. How do you loop through the characters of a string 'hello'?

a) for char in 'hello':
b) for char in range(len('hello')):
c) for char in enumerate('hello'):
d) for char <= 'hello':

Answer:

a) for char in 'hello':

Explanation:

You can loop through a string directly by iterating over its characters.

9. What is the output of the following code?

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

Answer:

a) 0 1 2 Done

Explanation:

The for loop iterates over 0, 1, 2, and then the else block is executed, printing "Done".

10. How can you loop through a tuple (1, 2, 3) and print each number?

a) for n in (1, 2, 3): print(n)
b) for n <= (1, 2, 3): print(n)
c) for n = 1 to 3: print(n)
d) for each n in (1, 2, 3): print(n)

Answer:

a) for n in (1, 2, 3): print(n)

Explanation:

You can iterate through a tuple by directly looping over its elements.

11. How do you create a nested for loop to iterate over a 2×2 matrix [[1,2],[3,4]]?

a) for i in matrix: for j in i: print(j)
b) for i in range(len(matrix)): for j in range(len(matrix[i])): print(matrix[i][j])
c) for [i, j] in matrix: print(i, j)
d) Both a and b

Answer:

d) Both a and b

Explanation:

Both methods correctly iterate over a nested list or matrix.

12. What is the purpose of the 'else' clause in a for loop?

a) To execute code when the loop condition is initially False
b) To execute code when the loop exits without encountering a break
c) To add an additional condition to the loop
d) To execute code at the end of each iteration

Answer:

b) To execute code when the loop exits without encountering a break

Explanation:

In Python, the else block in a for loop is executed when the loop completes normally without a break.

Leave a Comment

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

Scroll to Top