Week 6 — Quiz (auto-graded) · Loops II: `for`, `range` & Nested Loops
Course: Introduction to Computer Science — CS1 / Programming Fundamentals in Python (CSCI 1101) · Silver Oak University (fictional sample) · Prof. Okafor
Objective tested: Objective 4 — the for loop; range() (start / stop / step, with an exclusive stop); accumulating; nested loops and predicting their output / line count; an off-by-one debug.
Points: 10 (1 each) · Assignment group: Quizzes (10% of grade) · Due: end of Module 6.
This is the human-readable quiz with its vetted answer key and feedback. The import-ready Classic QTI is in
F-quiz-week-06-qti.xml(generated by the shared validated script — parses with 10 items, every single-answer item exactly one correct). Execution gate: PASS — every "what does this print?" key below, everyrange(...)listing, and the matching pairs were produced by actually running the code in Python, not hand-traced. The Canvas placement block is at the bottom of this file.
Blueprint
| # | Type | Concept | Objective |
|---|---|---|---|
| 1 | Multiple choice | for vs while — when a for loop is the right tool |
4 |
| 2 | Multiple choice | Predict the output — for i in range(3) (0-based) |
4 |
| 3 | Multiple choice | Predict the output — range(1, 5) and the exclusive stop |
4 |
| 4 | Multiple choice | Predict the output — step (range(0, 10, 2)) |
4 |
| 5 | Multiple choice | Nested loop — how many lines does it print? | 4 |
| 6 | Multiple answer | True statements about for / range / nesting |
4 |
| 7 | Multiple choice | Debugging — an off-by-one from the exclusive stop in an accumulator | 4 |
| 8 | True / False | "range(1, 5) includes 5" misconception |
4 |
| 9 | Multiple choice | Predict the output — negative step countdown | 4 |
| 10 | Matching | range(...) call → the values it yields (each run-verified) |
4 |
No trick questions; distractors are plausible mis-traces (including the stop value, starting at 1 instead of 0, miscounting nested-loop lines).
Questions, key, and feedback
Q1 (MC). Which statement best describes when a for loop is the natural choice over a while loop?
- A. When you must repeat as long as a condition stays true and don't know how many times in advance
- B. When you want to repeat a known number of times, or step through each item in a sequence ✅
- C. When you want the loop to run forever
- D. When you need to make a decision but not repeat anything
Feedback: Reach for for when the count is known or you're walking a sequence (a string's characters, the numbers from range()). Reach for while when you repeat until some condition changes and can't predict the number of passes (A). (C describes a bug; D describes an if.)
Q2 (MC). What does this program print?
for i in range(3):
print(i)
- A.
1 2 3 - B.
0 1 2 3 - C.
0,1,2(on three separate lines) ✅ - D.
0 1 2(on one line)
Feedback:range(3)produces0, 1, 2— it starts at 0 and stops before 3 (three values). Eachprint(i)is on its own line. (A wrongly starts at 1; B wrongly includes 3; D wrongly puts them on one line. Run-verified.)
Q3 (MC). What does this program print on one line?
for i in range(1, 5):
print(i, end=' ')
- A.
1 2 3 4 5 - B.
1 2 3 4✅ - C.
0 1 2 3 4 - D.
1 2 3 4 5 6
Feedback: The stop is exclusive.range(1, 5)goes1, 2, 3, 4and stops before 5 — the 5 never prints. This is the #1 off-by-one source in programming. To include the 5, you'd writerange(1, 6). (Run-verified:1 2 3 4.)
Q4 (MC). What does this program print on one line?
for i in range(0, 10, 2):
print(i, end=' ')
- A.
0 2 4 6 8 10 - B.
0 1 2 3 4 5 6 7 8 9 - C.
2 4 6 8 10 - D.
0 2 4 6 8✅
Feedback: The third number is the step: count by 2 from 0, stopping before 10 — so0, 2, 4, 6, 8. The stop stays exclusive even with a step, so 10 is not included (A is the classic mistake). (Run-verified:0 2 4 6 8.)
Q5 (MC). How many lines does this program print?
for r in range(3):
for c in range(2):
print('*', end='')
print()
- A. 6 lines
- B. 2 lines
- C. 3 lines ✅
- D. 1 line
Feedback: The outer loop runs 3 times — once per row — and theprint()that ends each row is indented under the outer loop, so 3 lines (**,**,**). The innerprint('*', end='')keeps the two stars on the same line. (If the line-endingprint()were under the inner loop, you'd get more lines. Run-verified: 3 lines.)
Q6 (Multiple answer — select all that apply). Which of the following statements are true?
- A. range(stop) starts at 0 ✅
- B. In range(start, stop), the stop value is NOT included ✅
- C. range(0, 10, 2) includes 10
- D. In a nested loop, the inner loop runs completely for each step of the outer loop ✅
- E. A for loop must always be given a condition to check, like a while loop
Feedback: range(stop) is 0-based (A); the stop is excluded (B); and the inner loop runs fully on every outer step (D). C is false — range(0, 10, 2) is 0, 2, 4, 6, 8; 10 is excluded. E is false — a for loop iterates a sequence and asks no condition (that's why it can't run forever like a while).
Q7 (MC). A student wants to add up 1 + 2 + 3 + 4 + 5 and writes this. What does it actually print, and what's the bug?
total = 0
for n in range(1, 5):
total += n
print(total)
- A.
15— it works correctly - B.
10—range(1, 5)stops at 4, so it misses the 5; userange(1, 6)✅ - C. It crashes with an error
- D.
5— it only adds the last number
Feedback:range(1, 5)is1, 2, 3, 4, so the loop sums1 + 2 + 3 + 4 = 10and silently misses the 5 — an off-by-one. It does not crash; it just gives a wrong answer. The fix isrange(1, 6)(then it prints15). (Run-verified: this code prints10; the fix prints15.)
Q8 (True / False). "range(1, 5) produces the numbers 1, 2, 3, 4, and 5."
- True
- False ✅
Feedback: False. The stop value is exclusive: range(1, 5) is 1, 2, 3, 4 — it stops before 5. To include 5, use range(1, 6). When unsure, run print(list(range(1, 5))) and read what Python actually produces.
Q9 (MC). What does this program print on one line?
for i in range(5, 0, -1):
print(i, end=' ')
- A.
5 4 3 2 1 0 - B.
1 2 3 4 5 - C.
5 4 3 2 1✅ - D.
0 1 2 3 4 5
Feedback: A negative step counts down: start at 5, step-1, stop before 0 — so5, 4, 3, 2, 1. The exclusive-stop rule holds going down too, so 0 is not printed (A is the trap). (Run-verified:5 4 3 2 1.)
Q10 (Matching). Match each range(...) call to the exact sequence of values it produces.
| range(...) call | Correct values |
|---|---|
| range(3) | 0 1 2 |
| range(1, 4) | 1 2 3 |
| range(0, 10, 2) | 0 2 4 6 8 |
| range(5, 0, -1) | 5 4 3 2 1 |
Feedback: range(3) → 0 1 2 (0-based, stops before 3); range(1, 4) → 1 2 3 (stops before 4); range(0, 10, 2) → 0 2 4 6 8 (step 2, stops before 10); range(5, 0, -1) → 5 4 3 2 1 (counts down, stops before 0). Every pair was run-verified with print(*range(...)).
Answer key (quick reference)
| Q | Answer |
|---|---|
| 1 | B |
| 2 | C (0 / 1 / 2) |
| 3 | B (1 2 3 4) |
| 4 | D (0 2 4 6 8) |
| 5 | C (3 lines) |
| 6 | A, B, D |
| 7 | B (10; fix = range(1, 6)) |
| 8 | False |
| 9 | C (5 4 3 2 1) |
| 10 | range(3)→0 1 2 / range(1,4)→1 2 3 / range(0,10,2)→0 2 4 6 8 / range(5,0,-1)→5 4 3 2 1 |
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 range(...) calls to four distinct value sequences. Execution gate: PASS — the keys for the predict-the-output items (Q2 0 1 2; Q3 1 2 3 4; Q4 0 2 4 6 8; Q9 5 4 3 2 1), the nested-loop line count (Q5 = 3), the off-by-one debug (Q7 = 10, fix 15), and all four matching pairs (Q10) were each produced by running the code in Python, not hand-traced. Distractors are plausible mis-traces (Q3/Q8 including the stop; Q2 starting at 1; Q4/Q9 including the excluded endpoint; Q5 miscounting lines).
Item-bank entries (for variants + the midterm/final)
All ten items are tagged course=CSCI1101 · week=6 · objective=4 · topic=for-range-nested-loops and deposited in Item Bank: Week 6 — Loops II (for/range/nested). The midterm (Week 8) and the per-term variant updates draw fresh items from this bank. (Tags: q1 for-vs-while, q2 range-zero-based, q3 exclusive-stop, q4 step, q5 nested-linecount, q6 true-statements, q7 debug-offbyone, q8 exclusive-stop-tf, q9 negative-step, q10 range-match.)
Canvas placement block
canvas_object = Quizzes::Quiz
title = "Week 6 Quiz — Loops II: for, range & nested loops"
assignment_group = "Quizzes"
points_possible = 10
grading_type = points
due_offset_days = 6 # 6 days after module start
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-06-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