Loops Questions and Answers

Practice Mode
Showing 10 of 25 questions
Q11
Which function is used to get both index and value in a loop?
  • A index()
  • B enumerate()
  • C range()
  • D iter()
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
  • A 5 4 3 2 1
  • B 5 4 3 2 1 0
  • C 4 3 2 1 0
  • D Infinite loop
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?
  • A break
  • B skip
  • C continue
  • D pass
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?
  • A Exit the loop
  • B Skip iteration
  • C Do nothing placeholder
  • D Pause execution
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?
  • A Using parallel()
  • B Using zip()
  • C Using merge()
  • D Using combine()
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")
  • A 0 1 Done
  • B 0 1
  • C Done
  • D 0 1 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?
  • A for loop
  • B while loop
  • C nested loop
  • D infinite loop
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?
  • A 0 1 2 3
  • B 1 2 3 4
  • C 0 1 2 3 4
  • D 1 2 3
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?
  • A for key in dict:
  • B for key in dict.keys():
  • C for key in dict.items():
  • D Both 1 and 2
Answer: Option D
Explanation: By default, for loops over a dictionary iterate through its keys.
Q20
What is nested loop in Python?
  • A Loop inside another loop
  • B Loop with multiple conditions
  • C Loop with break statement
  • D Loop with continue statement
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.
Questions and Answers for Competitive Exams Various Entrance Test