Chapter 6 Quiz: Loops and Iteration
Test your understanding of Pascal's loop constructs. Answers appear at the end.
Q1. Which Pascal loop construct guarantees that the loop body executes at least once?
A) WHILE..DO
B) FOR..TO
C) REPEAT..UNTIL
D) All of the above
Q2. In a REPEAT..UNTIL loop, when does the loop terminate?
A) When the condition becomes False
B) When the condition becomes True
C) After a fixed number of iterations
D) When a Break statement is reached
Q3. What is the output of the following code?
x := 3;
while x > 0 do
begin
Write(x, ' ');
x := x - 1;
end;
A) 3 2 1 0
B) 3 2 1
C) 2 1 0
D) 3 2 1 (infinite loop)
Q4. How many times does this loop execute?
for i := 5 to 5 do
WriteLn(i);
A) 0 times B) 1 time C) 5 times D) It produces a compile error
Q5. Which of the following is NOT a valid loop variable type for a FOR loop in Pascal?
A) Integer
B) Char
C) Real
D) Boolean
Q6. What is wrong with this code?
i := 1;
while i <= 10 do
WriteLn(i);
i := i + 1;
A) The variable i is not declared
B) The WriteLn should be Write
C) Missing begin..end around the two statements in the loop body
D) The condition should be i < 10
Q7. What is the output of the following code?
for i := 3 downto 1 do
Write(i * 2, ' ');
WriteLn;
A) 6 4 2
B) 2 4 6
C) 3 2 1
D) 6 4 2 0
Q8. Which loop construct is best suited for reading lines from a file that might be empty?
A) REPEAT..UNTIL
B) WHILE..DO
C) FOR..TO
D) Any of the three works equally well
Q9. What is a "sentinel value"?
A) A variable that guards against infinite loops B) A special value that signals the end of input C) The initial value of a loop counter D) A Boolean flag that controls loop execution
Q10. How many times does the inner WriteLn statement execute?
for i := 1 to 3 do
for j := 1 to 4 do
WriteLn(i, ',', j);
A) 3 times B) 4 times C) 7 times D) 12 times
Q11. In Free Pascal, what does the Break statement do inside a loop?
A) Pauses the loop until the user presses a key B) Exits the program entirely C) Exits the innermost enclosing loop D) Skips to the next iteration of the loop
Q12. What is the output of the following code?
n := 0;
repeat
n := n + 1;
until n >= 1;
WriteLn(n);
A) 0
B) 1
C) 2
D) It produces an infinite loop
Q13. Which of the following correctly computes the sum 1 + 2 + ... + 100?
A) for i := 0 to 100 do sum := sum + i; (with sum initialized to 0)
B) for i := 1 to 100 do sum := sum + i; (with sum initialized to 0)
C) for i := 1 to 99 do sum := sum + i; (with sum initialized to 0)
D) for i := 1 to 100 do sum := sum + 1; (with sum initialized to 0)
Q14. What does the Continue statement do in a loop?
A) Exits the loop immediately B) Restarts the loop from the beginning with the same values C) Skips the rest of the current iteration and proceeds to the next D) Continues to the statement after the loop
Q15. What is an "off-by-one error"?
A) A syntax error caused by misspelling a keyword B) A logic error where a loop executes one time too many or too few C) An error caused by dividing by zero D) A runtime error caused by accessing invalid memory
Q16. What is the value of count after this code executes?
count := 0;
for i := 1 to 10 do
if i mod 2 = 0 then
count := count + 1;
A) 2 B) 4 C) 5 D) 10
Q17. Why should you NOT modify the loop variable inside a FOR loop body?
A) It causes a compile error in all Pascal compilers B) It results in undefined behavior according to the Pascal standard C) It will always create an infinite loop D) It makes the loop run backwards
Q18. Which pattern describes a loop that accumulates a running total?
A) Counter pattern B) Accumulator pattern C) Sentinel pattern D) Search pattern
Q19. What is the output of the following code?
for i := 10 to 5 do
WriteLn(i);
WriteLn('Done');
A) 10 9 8 7 6 5 Done (each on a separate line)
B) Done (only)
C) It produces a runtime error
D) It produces an infinite loop
Q20. Which debugging technique is described as "tracing through a loop by hand with a small input, writing down each variable's value at each iteration"?
A) Rubber duck debugging B) Desk-checking C) Printf debugging D) Breakpoint analysis
Answer Key
Q1. C — REPEAT..UNTIL is a post-test loop; the body always executes before the condition is checked.
Q2. B — The loop terminates when the UNTIL condition evaluates to True. Think of UNTIL as "stop when true."
Q3. B — 3 2 1. When x becomes 0, the condition x > 0 is False, and the loop exits without printing 0.
Q4. B — 1 time. When the start and end values are equal, the body executes exactly once.
Q5. C — Real is not an ordinal type. FOR loop variables must be ordinal (Integer, Char, Boolean, or enumerated types).
Q6. C — Without begin..end, only WriteLn(i) is inside the loop. The i := i + 1 line is after the loop, so i is never incremented, causing an infinite loop printing 1 forever.
Q7. A — 6 4 2. The loop counts from 3 down to 1, and each value is multiplied by 2.
Q8. B — WHILE..DO can execute zero times if the file is empty (i.e., Eof is True immediately). REPEAT..UNTIL would attempt to read from an empty file.
Q9. B — A sentinel value is a special value that marks the end of a data sequence, like entering -1 to stop input.
Q10. D — 12 times. The outer loop runs 3 times, and for each outer iteration, the inner loop runs 4 times: 3 x 4 = 12.
Q11. C — Break exits the innermost loop containing the statement. It does not affect outer loops.
Q12. B — The body executes once, incrementing n from 0 to 1. Then n >= 1 is True, so the loop exits. Output is 1.
Q13. B — Option A starts from 0 (which adds 0, giving the correct sum but conceptually includes an extra iteration). Option B correctly sums from 1 to 100. Option C stops at 99 (missing 100). Option D counts iterations rather than summing i.
Q14. C — Continue skips the remaining statements in the current iteration and proceeds to the loop's condition check (or next iteration for FOR).
Q15. B — Off-by-one errors occur when a loop iterates one time too many or too few, often due to using < instead of <= (or vice versa) in the condition.
Q16. C — 5. The even numbers from 1 to 10 are 2, 4, 6, 8, 10 — five numbers.
Q17. B — The Pascal standard states that modifying the loop variable inside a FOR loop body results in undefined behavior. The compiler manages the loop variable; interfering with it is unreliable.
Q18. B — The accumulator pattern uses a variable (initialized before the loop) that accumulates a running total by adding a value each iteration.
Q19. B — Since the start value (10) is greater than the end value (5) in a FOR..TO loop, the body executes zero times. Only "Done" is printed.
Q20. B — Desk-checking (also called hand-tracing) involves manually simulating the program's execution on paper with a small set of values.