Python Loops MCQ Questions and Answers
Q. What is the type of loop that is used to execute a block of code as long as a certain condition is true?
A. while loopB. for loop
C. do-while loop
D. All of the above
Q. What is the difference between a "while" loop and a "for" loop in Python?
A. while loop checks the condition at the beginning, for loop checks at the endB. for loop requires an iterator, while loop does not
C. while loop is used for infinite loops, for loop is not
D. All of the above
Q. How can you create an infinite loop using a "while" loop in Python?
A. while True:B. while 1:
C. for _ in range(infinite):
D. None of the above
Q. What is the purpose of the "break" statement in a loop?
A. To exit the loop immediatelyB. To skip the rest of the current iteration and continue to the next
C. To restart the loop from the beginning
D. To execute the loop one more time
Q. What is the purpose of the "continue" statement in a loop?
A. To exit the loop immediatelyB. To skip the rest of the current iteration and move to the next
C. To restart the loop from the beginning
D. To execute the current iteration one more time
Q. How can you iterate over a list using a "for" loop in Python?
A. for i in list:B. for each item in list:
C. for (i = 0; i < len(list); i++)
D. for i = 0 to len(list)-1:
Q. What is the output of the following code snippet?
n = 5
for i in range(n):
print(i)
A. 0 1 2 3 4B. 1 2 3 4 5
C. 0 1 2 3
D. None of the above
Q. How can you iterate over both the index and the value of a list in a "for" loop?
A. for i, value in enumerate(list):B. for i in range(len(list)):
C. for i, value in zip(range(len(list)), list):
D. for i, value in iter(list):
Q. What is the output of the following code snippet?
numbers = [1, 2, 3, 4, 5]
total = 0
for num in numbers:
total += num
print(total)
A. 1B. 2
C. 15
D. None of the above
Q. How can you nest one loop inside another loop in Python?
A. by using different loop variablesB. by using the same loop variable
C. by using the "nested" keyword
D. by using the "inner" keyword
Q. What is the purpose of the "else" clause in a "for" loop?
A. To execute code when the loop is exited due to a "break" statementB. To execute code when the loop completes normally
C. To specify an alternate set of code to execute
D. To provide a condition for the loop
Q. How can you use the "pass" statement in a loop?
A. To do nothing and continue to the next iterationB. To skip the rest of the current iteration and move to the next
C. To exit the loop immediately
D. To execute the loop indefinitely
Q. What is the output of the following code snippet?
for letter in "Python":
if letter == "h":
continue
print(letter)
A. PB. yt
C. ython
D. None of the above
Q. How can you use the "range()" function to generate a sequence of odd numbers?
A. range(1, 10, 2)B. range(0, 10, 2)
C. range(1, 10, 3)
D. range(0, 10, 3)
Q. What is the purpose of the "else" clause in a "while" loop?
A. To execute code when the loop is exited due to a "break" statementB. To execute code when the loop completes normally
C. To specify an alternate set of code to execute
D. To provide a condition for the loop