Week 12 — Quiz (auto-graded) · File I/O & Exceptions
Course: Introduction to Computer Science — CS1 / Programming Fundamentals in Python (CSCI 1101) · Silver Oak University (fictional sample) · Prof. Okafor
Objective tested: Objective 7 — files (with open), try/except, common exceptions.
Points: 10 (1 each) · Assignment group: Quizzes (10% of grade) · Due: end of Module 12.
Human-readable quiz with its vetted answer key. Import-ready Classic QTI in
F-quiz-week-12-qti.xml(parses with 10 items, one correct per single-answer item). Execution gate: PASS — every keyed output/error was produced by running the code.
Questions, key, and feedback
Q1 (MC). Which line opens a file for writing and closes it automatically when the block ends?
- A. open("f.txt")
- B. with open("f.txt", "w") as f: ✅
- C. read("f.txt")
- D. file f.txt write
Feedback: with opens and auto-closes the file; "w" is write mode. (A opens for reading and doesn't auto-close; C/D aren't valid.)
Q2 (MC). What does this print?
with open("f.txt", "w") as f:
f.write("hi")
with open("f.txt") as f:
print(f.read())
- A.
f.txt - B.
hi✅ - C.
(nothing) - D. an error
Feedback: You wrotehito the file, then.read()returned its contents as a string:hi. (Run-verified.)
Q3 (MC). What does this print?
try:
print(int("abc"))
except ValueError:
print("not a number")
- A.
abc - B.
not a number✅ - C.
0 - D. the program crashes
Feedback:int("abc")raises aValueError, so Python skips to the matchingexceptand printsnot a number— no crash. (Run-verified.)
Q4 (MC). Opening a file that does not exist raises which exception?
- A. ValueError
- B. KeyError
- C. FileNotFoundError ✅
- D. ZeroDivisionError
Feedback: A missing file is a FileNotFoundError — the name describes it literally. (Run-verified last line: FileNotFoundError: [Errno 2] No such file or directory.)
Q5 (Multiple answer — select all that apply). Which statements are true?
- A. with closes the file automatically ✅
- B. A file read with .read() returns an int if the file holds digits
- C. "w" mode overwrites the file (starts it empty) ✅
- D. except catches only the exception type you name ✅
- E. except ValueError will catch a FileNotFoundError
Feedback: with auto-closes (A); "w" overwrites (C); except catches only the named type (D). B is false — a read returns a string. E is false — different types.
Q6 (Matching). Match each exception to its cause.
| Exception | Cause |
|---|---|
| FileNotFoundError | Opening a file that does not exist |
| ValueError | Converting text that isn't a valid number |
| ZeroDivisionError | Dividing a number by zero |
| KeyError | Looking up a dictionary key that isn't there |
Feedback: Match the exception to what triggers it — the type name usually says it (a file not found, a bad value, a zero division, a missing key).
Q7 (MC). What does this print?
try:
x = 6 / 2
print("ok:", x)
except ZeroDivisionError:
print("div0")
- A.
ok: 3.0✅ - B.
div0 - C.
ok: 3 - D. an error
Feedback: Thetryblock succeeds, so theexceptis skipped; and/always gives a float, so6 / 2is3.0. (Run-verified.)
Q8 (True / False). "except ValueError: will catch a FileNotFoundError."
- True
- False ✅
Feedback: False. except catches only the type you name. A missing file is a FileNotFoundError, which except ValueError does not catch.
Q9 (MC). Using with open(...) as f: instead of a plain open(...) is recommended mainly because with —
- A. automatically closes the file for you when the block ends ✅
- B. makes the file load faster
- C. converts the file's text to numbers
- D. prevents all possible errors
Feedback: with guarantees the file is closed (even if an error occurs), so you never forget f.close(). (It doesn't speed up loading, convert types, or prevent errors.)
Q10 (MC). This program crashes when data.txt is missing:
with open("data.txt") as f:
print(f.read())
What is the correct fix?
- A. Change "data.txt" to "data"
- B. Wrap it in try: … except FileNotFoundError: to handle the missing file ✅
- C. Add a semicolon at the end
- D. Nothing — it can't be fixed
Feedback: Wrap the risky open in a try / except FileNotFoundError so a missing file prints a friendly message instead of crashing. (Run-verified: the wrapped version handles it cleanly.)
Answer key (quick reference)
| Q | Answer | Q | Answer |
|---|---|---|---|
| 1 | B | 6 | FileNotFoundError→missing file / ValueError→bad conversion / ZeroDivisionError→/0 / KeyError→missing key |
| 2 | B (hi) |
7 | A (ok: 3.0) |
| 3 | B (not a number) |
8 | False |
| 4 | C (FileNotFoundError) | 9 | A |
| 5 | A, C, D | 10 | B |
Quality gate (self-checked): each single-answer item has exactly one correct option; Q5 keys A, C, D (B, E left unselected); Q6 pairs four exceptions to four causes. Execution gate: PASS — Q2 (hi), Q3 (not a number), Q7 (ok: 3.0), and the Q4/Q6/Q8/Q10 exception behaviors were each produced by running the code in Python.
Canvas placement block
canvas_object = Quizzes::Quiz
title = "Week 12 Quiz — File I/O & Exceptions"
assignment_group = "Quizzes"
points_possible = 10
grading_type = points
due_offset_days = 6
published = true
shuffle_answers = true
ai_permitted = false
provenance = "~ Prof. Okafor's edition · Fall 2026 · built with thecoursemaker.com"
F-quiz-week-12-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