Loops Questions and Answers
Practice ModeShowing 10 of 25 questions
Q11
Which function is used to get both index and value in a loop?
Answer: Option B
Explanation: enumerate() returns both the index and the value during iteration over a sequence.
Q12
What will this code output: x = 5
while x > 0:
print(x)
x -= 1
Answer: Option A
Explanation: The loop starts at 5, decrements by 1 each iteration, and stops when x becomes 0.
Q13
Which statement is used to skip the rest of the code in the current iteration?
Answer: Option C
Explanation: The continue statement skips the remaining code in the current iteration and moves to the next iteration.
Q14
What is the purpose of the pass statement in loops?
Answer: Option C
Explanation: pass is a null operation used as a placeholder where syntax requires a statement but no action is needed.
Q15
How to iterate over two lists simultaneously in Python?
Answer: Option B
Explanation: zip() function pairs elements from multiple sequences and allows simultaneous iteration.
Q16
What is the output of: for i in range(2): print(i, end=" ") else: print("Done")
Answer: Option A
Explanation: The loop prints 0 1, then the else clause executes since no break occurred.
Q17
Which loop is more suitable when the number of iterations is unknown?
Answer: Option B
Explanation: while loop is ideal when the number of iterations depends on a condition that may change during execution.
Q18
What does range(4) generate?
Answer: Option A
Explanation: range(4) generates numbers from 0 up to but not including 4: 0, 1, 2, 3.
Q19
How to iterate over dictionary keys in Python?
Answer: Option D
Explanation: By default, for loops over a dictionary iterate through its keys.
Q20
What is nested loop in Python?
Answer: Option A
Explanation: A nested loop is a loop inside another loop, where the inner loop completes all iterations for each iteration of the outer loop.