Week 5 — Quiz (auto-graded) · Loops I: the `while` loop
Course: Introduction to Computer Science — CS1 / Programming Fundamentals in Python (CSCI 1101) · Silver Oak University (fictional sample) · Prof. Okafor
Objective tested: Objective 4 — while loops; counters & accumulators; predicting a loop's final value and iteration count; off-by-one (< vs <=); infinite-loop debugging; break.
Points: 10 (1 each) · Assignment group: Quizzes (10% of grade) · Due: end of Module 5.
This is the human-readable quiz with its vetted answer key and feedback. The import-ready Classic QTI is in
F-quiz-week-05-qti.xml(generated by the shared validated script — parses with 10 items, every single-answer item exactly one correct). Execution gate: PASS — every final value, accumulator total, iteration count, and loop output keyed below was produced by actually running the code in Python, not hand-traced. (The infinite-loop item, Q4, is debugged by reading; the fixed version's output is run-verified.) The Canvas placement block is at the bottom of this file.
Blueprint
| # | Type | Concept | Objective |
|---|---|---|---|
| 1 | Multiple choice | Predict the final value of a counter (count after while count < 4) |
4 |
| 2 | Multiple choice | Predict the output of a while loop (step of 2) |
4 |
| 3 | Multiple choice | Accumulator — predict the running total (sum 1..6) | 4 |
| 4 | Multiple choice | Debugging — infinite loop: why it never stops + the fix | 4 |
| 5 | Multiple answer | True statements about while loops, counters & accumulators |
4 |
| 6 | Matching | Loop snippet → final value (four loops, each run-verified) | 4 |
| 7 | Multiple choice | Off-by-one — which loop prints 1 through 5 inclusive | 4 |
| 8 | True / False | "After while count <= 5, count ends at 5" misconception |
4 |
| 9 | Multiple choice | Iteration count — how many times a loop runs (step of 2) | 4 |
| 10 | Multiple choice | Predict the output — a loop with break |
4 |
No trick questions; distractors are plausible mis-traces (off-by-one, counting the boundary wrong, ending value off by one, forgetting break fires).
Questions, key, and feedback
Q1 (MC). What does this program print?
count = 0
while count < 4:
count = count + 1
print(count)
- A.
3 - B.
4✅ - C.
5 - D. nothing (it never stops)
Feedback: The loop runs whilecount < 4, adding 1 each pass: 0→1→2→3→4. Whencountreaches 4,4 < 4is False, so it stops, andprintshows4. (A3is the off-by-one of stopping one early; the loop does terminate, so D is wrong. Run-verified:4.)
Q2 (MC). What does this program print?
i = 2
while i < 10:
print(i)
i = i + 2
- A.
2 4 6 8 10(each on its own line) - B.
2 4 6 8(each on its own line) ✅ - C.
2 3 4 5 6 7 8 9 - D.
2 4 6 8 10 12
Feedback: Starting at 2 and stepping by 2 whilei < 10: it prints 2, 4, 6, 8. Whenibecomes 10,10 < 10is False, so 10 is not printed (<excludes the boundary). (A includes 10 — the off-by-one. C steps by 1. Run-verified:2 4 6 8.)
Q3 (MC). This loop adds up the numbers 1 through 6. What does it print?
total = 0
n = 1
while n <= 6:
total = total + n
n = n + 1
print(total)
- A.
15 - B.
21✅ - C.
6 - D.
28
Feedback: The accumulator builds 1+2+3+4+5+6 = 21.totalstarts at 0; each pass adds the currentn(1 through 6, because<= 6includes 6). (A15is the sum 1..5 — off by one. D28is 1..7. Run-verified:21.)
Q4 (MC). This program is supposed to print 1, 2, 3, 4 but it never stops:
count = 1
while count < 5:
print(count)
Why does it never stop, and what single line fixes it?
- A. The condition should be count <= 5; change < to <=
- B. The counter is never updated, so count < 5 stays True forever; add count = count + 1 inside the loop ✅
- C. while loops always run forever; use if instead
- D. The print is indented wrong; move it outside the loop
Feedback: This is an infinite loop — the classic missing STEP. Because count never changes, count < 5 is always True, so it prints 1 endlessly. The fix is to update the counter inside the body: add count = count + 1. (If one ever runs for real, press Ctrl+C or Stop to interrupt it.) Changing the condition (A) doesn't help — it'd still never update count. With the fix, it prints 1 2 3 4 — run-verified.
Q5 (Multiple answer — select all that apply). Which of the following statements are true?
- A. A while loop repeats its body as long as its condition is True ✅
- B. An accumulator should be initialized (e.g., total = 0) before the loop ✅
- C. A while loop always runs exactly once
- D. Forgetting to update the counter inside the loop can cause an infinite loop ✅
- E. while i < 5 and while i <= 5 always run the same number of times
Feedback: A while loop repeats while its condition holds (A); accumulators start before the loop (B); a missing counter update causes an infinite loop (D). C is false — a while loop can run zero, one, or many times depending on the condition. E is false — <= 5 runs one more iteration than < 5 (the off-by-one).
Q6 (Matching). Match each loop snippet to the value it prints (every pairing is run-verified).
| Loop snippet | Prints |
|---|---|
| c = 0 / while c < 3: / c += 1 / print(c) | 3 |
| t = 0 / i = 1 / while i <= 3: / t += i / i += 1 / print(t) | 6 |
| i = 0 / while i < 6: / i += 2 / print(i) | 6 (the loop variable's final value) |
| n = 4 / last = 0 / while n > 0: / last = n / n -= 1 / print(last) | 1 |
Feedback: (1) counts 0→3, prints 3. (2) accumulates 1+2+3 = 6. (3) steps 0→2→4→6; at 6, 6 < 6 is False, so it stops with i = 6. (4) counts down 4,3,2,1, remembering the last value seen, which is 1. (Two snippets print 6 for different reasons — a counter's end value vs. an accumulator's total — which is the point.)
Q7 (MC). You want a loop that prints the numbers 1, 2, 3, 4, 5 (including 5). Each option starts with i = 1, prints i, then does i = i + 1. Which condition is correct?
i = 1
while CONDITION:
print(i)
i = i + 1
- A.
while i < 5: - B.
while i <= 5:✅ - C.
while i > 5: - D.
while i < 4:
Feedback:while i <= 5includes 5, so it prints 1 through 5. Awhile i < 5stops at 4 — that's the off-by-one that silently drops the 5.while i > 5is False immediately (prints nothing);while i < 4prints only 1–3. (Run-verified:i <= 5prints1 2 3 4 5.)
Q8 (True / False). "After this loop runs, count is 5."
count = 1
while count <= 5:
count = count + 1
- True
- False ✅
Feedback: False.countends at 6, not 5. The loop adds 1 and then re-checks the condition: whencountis 5,5 <= 5is True, so it runs once more and steps to 6; then6 <= 5is False and it stops. The counter always ends one past the boundary. (Run-verified: printingcountafter the loop shows6.)
Q9 (MC). How many times does the body of this loop run (how many lines does it print)?
i = 0
while i <= 10:
print(i)
i = i + 2
- A. 5 times
- B. 6 times ✅
- C. 10 times
- D. 11 times
Feedback: It prints0, 2, 4, 6, 8, 10— that's 6 values (<= 10includes 10). Thenibecomes 12 and12 <= 10is False, so it stops. (A5forgets to count 0; D11counts every integer. Run-verified: 6 lines.)
Q10 (MC). What does this program print?
n = 1
while True:
if n * n > 50:
print(n)
break
n = n + 1
- A. nothing (it runs forever)
- B.
7 - C.
8✅ - D.
50
Feedback:while Truelooks infinite, but thebreakends it. The loop stepsnupward and stops the first timen * n > 50: 7×7 = 49 is not over 50, but 8×8 = 64 is, so it prints8and breaks. (B7forgets that 49 isn't over 50; A forgets thebreakis the STOP. Run-verified:8.)
Answer key (quick reference)
| Q | Answer |
|---|---|
| 1 | B (4) |
| 2 | B (2 4 6 8) |
| 3 | B (21) |
| 4 | B (missing counter update; add count = count + 1) |
| 5 | A, B, D |
| 6 | c<3→3 / sum 1..3→6 / i<6 step 2→6 / countdown last→1 |
| 7 | B (while i <= 5:) |
| 8 | False (count ends at 6) |
| 9 | B (6 times) |
| 10 | C (8) |
Quality gate (self-checked): each single-answer item has exactly one correct option; the multiple-answer item keys A, B, D (and requires C and E unselected); the matching item pairs four loops to their run-verified printed values (with two pairs legitimately equal to 6). Execution gate: PASS — the keys for the predict/accumulator/iteration items (Q1 4; Q2 2 4 6 8; Q3 21; Q6 3/6/6/1; Q9 6 lines; Q10 8) and the off-by-one items (Q7, Q8 count ends 6) were each produced by running the code in Python, not hand-traced. The infinite-loop item (Q4) is diagnosed by reading the buggy code; its fixed form (1 2 3 4) is run-verified. Distractors are plausible mis-traces (Q2 2 4 6 8 10 = including the boundary; Q3 15 = summing 1..5; Q8 5 = forgetting the step-then-recheck).
Item-bank entries (for variants + the midterm/final)
All ten items are tagged course=CSCI1101 · week=5 · objective=4 · topic=while-loops-counters-accumulators and deposited in Item Bank: Week 5 — Loops I (while). The midterm (Week 8) and the per-term variant updates draw fresh items from this bank. (Tags: q1 final-value, q2 predict-step2, q3 accumulator-sum, q4 infinite-loop-debug, q5 true-statements, q6 snippet-match, q7 off-by-one, q8 ends-one-past, q9 iteration-count, q10 break.)
Canvas placement block
canvas_object = Quizzes::Quiz
title = "Week 5 Quiz — Loops I: the while loop"
assignment_group = "Quizzes"
points_possible = 10
grading_type = points
due_offset_days = 5 # 5 days after module start (Sun Oct 4)
published = true
shuffle_answers = true
ai_permitted = false # AI is not permitted on quizzes
provenance = "~ Prof. Okafor's edition · Fall 2026 · built with thecoursemaker.com"
F-quiz-week-05-qti.xml) ships inside the course's .imscc package — it lands in the Canvas gradebook on import.~ Prof. Okafor's edition · Fall 2026 · built with thecoursemaker.com