π Computational Thinking & Programming-I Quiz 9 β Loops & Iteration (Class 11 CS)
Practice 20 CBSE Class 11 Computer Science (Code 083) MCQs on iterative statements β for loops, range(), while loops, break & continue, nested loops, and classic beginner programs like pattern generation, summation of a series, and factorial.
Every code example below has been verified by actually running it in Python β including fixes to two questions from the original quiz that had duplicate or contradictory options. Tap any question to reveal the answer with a clear explanation.
π For Loop
Which iterative statement is used to execute a block of code a specific, known number of times?
- for loop
- while loop
- if statement
- switch statement
Show Answer & Explanation
β Answer: (a) for loop
A for loop is the natural choice when you already know how many times to repeat something β e.g. iterating a fixed number of times using range(), or once per item in a list.
π’ range()
What is the purpose of the range() function in Python?
- To generate a sequence of numbers
- To perform mathematical calculations
- To create a list of values directly
- To define the limits of a loop only visually
Show Answer & Explanation
β Answer: (a) To generate a sequence of numbers
range() generates a sequence of numbers β commonly used to control how many times a for loop runs, or to produce a specific numeric pattern.
π For Loop
Which of the following correctly shows the different valid forms of range() usable in a for loop’s syntax?
- for i in range(stop):
- for i in range(start, stop):
- for i in range(start, stop, step):
- All of the above are valid forms
Show Answer & Explanation
β Answer: (d) All of the above are valid forms
range() is flexible β it accepts 1, 2, or 3 arguments: just a stop value, a start and stop, or a start, stop, and step. All three forms shown are valid Python syntax. (This question is corrected from the original, whose four options included two identical duplicates of the same 3-argument form β leaving no way to answer correctly as originally written.)
π While Loop
What is the purpose of the while loop?
- To execute a block of code a fixed, predetermined number of times
- To execute a block of code repeatedly as long as a condition remains true
- To terminate the program immediately
- To define the limits of a for loop
Show Answer & Explanation
β Answer: (b) To execute a block of code repeatedly as long as a condition remains true
A while loop keeps repeating its block as long as its condition stays true β ideal when you don’t know in advance exactly how many repetitions will be needed.
π Loop Control
Which statement is used to terminate a loop prematurely, stopping it entirely before all iterations complete?
- return statement
- exit statement
- break statement
- continue statement
Show Answer & Explanation
β Answer: (c) break statement
break exits the loop immediately and completely β no further iterations run at all, and execution jumps to the code right after the loop.
π Loop Control
Which statement is used to skip the rest of the current iteration and move on to the next one, without exiting the loop?
- return statement
- exit statement
- break statement
- continue statement
Show Answer & Explanation
β Answer: (d) continue statement
continue stops the current pass through the loop body right where it is, and jumps to the next iteration β unlike break, the loop itself keeps running.
πͺ Nested Loops
What is a nested loop?
- A loop that executes infinitely
- A loop placed inside another loop
- A loop that terminates prematurely
- A loop that skips certain iterations
Show Answer & Explanation
β Answer: (b) A loop placed inside another loop
A nested loop is simply a loop written inside the body of another loop β commonly used for tasks like generating 2D patterns or grids, where the outer loop controls rows and the inner loop controls columns.
β Suggested Programs
Which of the following is a classic suggested program that relies on nested for loops?
- A program to check if a number is prime
- A program to calculate the square root of a number
- A program to generate a star or number pattern (like a triangle)
- A program to find the absolute value of a number
Show Answer & Explanation
β Answer: (c) A program to generate a star or number pattern (like a triangle)
Pattern-generation programs (printing triangles, pyramids of stars or numbers) are the textbook example of nested for loops β the outer loop handles each row, and the inner loop prints the characters within that row.
β Suggested Programs
What does a program that calculates the summation of a series of numbers using a for loop ultimately produce?
- The product of the series
- The average of the series
- The sum of the series
- The difference between consecutive terms
Show Answer & Explanation
β Answer: (c) The sum of the series
A summation program accumulates a running total inside the loop (e.g. total += i) and ends up with the sum of the series β as opposed to a product (which would need multiplication) or an average (which needs an extra division step).
π Loop Control
What is the key difference between break and continue in a loop?
- break exits the entire loop immediately; continue skips only the current iteration and moves to the next
- break skips only the current iteration; continue exits the entire loop
- They behave in exactly the same way
- Both statements immediately terminate the whole program
Show Answer & Explanation
β Answer: (a) break exits the entire loop immediately; continue skips only the current iteration and moves to the next
break stops the loop completely β no more iterations happen at all. continue just skips whatever’s left in the current iteration and lets the loop carry on to the next one. (This question replaces a self-contradictory one from the original quiz, whose wording β βexit a loop prematurely and move to the next iterationβ β actually describes break and continue’s behaviours mixed together, which doesn’t match either statement individually.)
π’ range()
What does range(2, 10, 2) generate in Python?
- [2, 4, 6, 8, 10]
- [2, 4, 6, 8]
- [2, 3, 4, …, 9]
- [0, 2, 4, 6, 8]
Show Answer & Explanation
β Answer: (b) [2, 4, 6, 8]
Starting at 2, stepping by 2, and stopping before 10 (exclusive), range(2, 10, 2) produces [2, 4, 6, 8].
π For Loop
What does the following program print?
total = 0
for i in range(1, 6):
total += i
print(total)- 10
- 15
- 21
- 6
Show Answer & Explanation
β Answer: (b) 15
This adds up 1 + 2 + 3 + 4 + 5 = 15. range(1, 6) produces 1 through 5 (6 is excluded).
π While Loop
What does the following program print? (It calculates a factorial.)
n = 5
fact = 1
i = 1
while i <= n:
fact *= i
i += 1
print(fact)- 25
- 120
- 15
- 20
Show Answer & Explanation
β Answer: (b) 120
This multiplies 1 Γ 2 Γ 3 Γ 4 Γ 5 = 120 β the factorial of 5, computed using a while loop that stops once i passes n.
πͺ Nested Loops
What does the following nested loop print, row by row?
for i in range(3):
row = ""
for j in range(i + 1):
row += "*"
print(row)- *, *, *
- *, **, ***
- ***, **, *
- * * *
Show Answer & Explanation
β Answer: (b) *, **, ***
The outer loop runs for i = 0, 1, 2. On each pass, the inner loop adds (i+1) stars, building a growing triangle: *, then **, then *** β a classic nested-loop pattern program.
π Loop Control
What does the following program print?
i = 0
while True:
if i == 3:
break
print(i)
i += 1- 0, 1, 2, 3
- 0, 1, 2
- 1, 2, 3
- An infinite loop that never stops
Show Answer & Explanation
β Answer: (b) 0, 1, 2
while True would normally loop forever, but the break stops it the moment i reaches 3 β so only 0, 1, 2 get printed before the loop exits.
π Loop Control
What does the following program print?
for i in range(5):
if i % 2 == 0:
continue
print(i)- 0, 2, 4
- 1, 3
- 0, 1, 2, 3, 4
- 1, 3, 5
Show Answer & Explanation
β Answer: (b) 1, 3
For every even i (0, 2, 4), continue skips the print() line entirely. Only the odd values actually get printed: 1, 3.
π’ range()
What does range(5, 1, -1) generate in Python?
- [5, 4, 3, 2, 1]
- [5, 4, 3, 2]
- [1, 2, 3, 4, 5]
- Error, since step can’t be negative
Show Answer & Explanation
β Answer: (b) [5, 4, 3, 2]
With a negative step, range() counts downward: starting at 5, decreasing by 1 each time, and stopping before it reaches 1 (exclusive): [5, 4, 3, 2].
πͺ Nested Loops
How many total times does the inner loop’s body run in the following code?
for i in range(3):
for j in range(4):
pass- 3
- 4
- 7
- 12
Show Answer & Explanation
β Answer: (d) 12
The outer loop runs 3 times, and for each of those, the inner loop runs 4 times β so the inner body executes 3 Γ 4 = 12 times in total.
π While Loop
What is the risk of writing while True: without ever including a break or a condition change inside the loop?
- Python automatically stops it after 1000 iterations
- It creates an infinite loop that never stops on its own
- It raises a SyntaxError immediately
- Nothing β Python optimizes it away
Show Answer & Explanation
β Answer: (b) It creates an infinite loop that never stops on its own
Since the condition True is always true, the loop has no natural way to stop β without a break (or something that eventually makes the condition false), it becomes an infinite loop, and the program will hang until manually interrupted.
β Suggested Programs
Which control structure is best suited for generating star or number patterns like triangles and pyramids?
- A single if statement
- A single while loop with no nesting
- Nested for loops (an outer loop for rows, an inner loop for columns)
- The break statement alone
Show Answer & Explanation
β Answer: (c) Nested for loops (an outer loop for rows, an inner loop for columns)
Nested for loops are the standard tool for pattern generation β the outer loop controls how many rows to print, and the inner loop controls how many characters go in each row.
