Loops Questions and Answers

Practice Mode
Showing 10 of 25 questions
Q1
Which loop in Python is used to iterate over a sequence of elements?
  • A while loop
  • B for loop
  • C do-while loop
  • D repeat loop
Answer: Option B
Explanation: The for loop in Python is specifically designed to iterate over sequences like lists, tuples, strings, and other iterable objects.
Q2
What is the output of: for i in range(3): print(i)
  • A 0 1 2
  • B 1 2 3
  • C 0 1 2 3
  • D 1 2
Answer: Option A
Explanation: range(3) generates numbers 0, 1, 2. The loop prints each number on separate lines.
Q3
Which keyword is used to exit a loop prematurely in Python?
  • A exit
  • B stop
  • C break
  • D return
Answer: Option C
Explanation: The break statement immediately terminates the loop and continues with the next statement after the loop.
Q4
What does the continue statement do in a Python loop?
  • A Exits the entire loop
  • B Skips current iteration
  • C Restarts the loop
  • D Pauses the loop
Answer: Option B
Explanation: The continue statement skips the current iteration and moves to the next iteration of the loop.
Q5
How many times will this loop execute: while False: print("Hello")
  • A 0 times
  • B 1 time
  • C Infinite times
  • D 2 times
Answer: Option A
Explanation: Since the condition is False from the beginning, the loop body never executes.
Q6
What is the output of: for i in range(1, 5, 2): print(i)
  • A 1 3
  • B 1 2 3 4
  • C 1 3 5
  • D 2 4
Answer: Option A
Explanation: range(1, 5, 2) starts at 1, ends before 5, with step 2, producing 1 and 3.
Q7
Which loop is guaranteed to execute at least once in Python?
  • A for loop
  • B while loop
  • C do-while loop
  • D None of the above
Answer: Option D
Explanation: Python does not have a built-in do-while loop. Both for and while may execute zero times depending on conditions.
Q8
What does the else clause with a for loop do in Python?
  • A Executes if loop encounters error
  • B Executes if loop completes normally
  • C Always executes
  • D Never executes
Answer: Option B
Explanation: The else clause in a for loop executes only if the loop completes normally without encountering a break statement.
Q9
How to create an infinite loop in Python?
  • A while 1:
  • B while True:
  • C for i in infinite:
  • D Both 1 and 2
Answer: Option D
Explanation: while True creates an infinite loop because the condition is always true.
Q10
What is the output of: for char in "Python": print(char, end=" ")
  • A P y t h o n
  • B Python
  • C P y t h o n
  • D P,y,t,h,o,n
Answer: Option A
Explanation: The loop iterates over each character in the string "Python" and prints them with spaces.
Questions and Answers for Competitive Exams Various Entrance Test