Standard difficulty · 20 questions · 40 marks · Time: 42 minutes
Show your working for Sections B and C. Answers are on the last page — try every question first!
Exception Handling in Python · AW-12CS-EXH-2026-V1 · For parents — fold back or tear off before handing the sheet to your child.
| Q | Answer | Working |
|---|---|---|
| 1 | (d) syntax error | — |
| 2 | (a) ValueError | the string cannot be read as a number |
| 3 | (a) Not a number, Done | int() of a word raises ValueError |
| 4 | (c) checked, 6 | finally runs before the function hands back its value |
| 5 | (a) 68, Marks out of range | once raise runs, the rest of the try block is skipped and except prints the message |
| 6 | (c) 114 3 | int() raises ValueError for a string with a decimal point, a word or an empty string |
| 7 | (a) 2 | only d = 0 raises ZeroDivisionError |
| 8 | (c) SyntaxError | the string is never closed |
| 9 | A syntax error breaks the rules of the language and stops the program from running at all; an exception happens while a correct program runs, e.g. dividing by zero. | — |
| 10 | IndexError | L has indices 0 to 2 only |
| 11 | 5, Bad index, Done | L has only indices 0 to 2, so L[8] raises IndexError after the division prints |
| 12 | (a) checked; (b) 9 | finally runs before the function hands back its value |
| 13 | (a) 140; (b) 2 | int() accepts only whole-number strings: one with a decimal point, a word or '' raises ValueError |
| 14 | 8, 24, skip, skip | else runs only when the division worked |
| 15 | 36, Marks out of range | raise jumps straight to the matching except |
| 16 | B, C, D | x / 0 raises ZeroDivisionError, so A and else are skipped; finally always runs; then the program carries on |
| 17 | try: a = int(input()); b = int(input()); c = a / b; except ZeroDivisionError: print('b cannot be 0'); except ValueError: print('Enter whole numbers'); else: print(c); finally: print('Done'). Only one except runs; else runs when nothing went wrong; finally always runs. | — |
| 18 | (a) Cannot divide, Done; (b) 17, skip, skip, 5 | n is 0, so // raises ZeroDivisionError |
| 19 | (a) checked, 10; (b) 27, Marks out of range; (c) always: after the try (and any except or else) part, whether or not an exception was raised, even when try returns. | finally runs on every path |
| 20 | (a) 74 3; (b) '', 'ab', '5.5'; (c) the first of them would raise an unhandled ValueError, the program would stop with a traceback and nothing would be printed. | int() accepts only strings of digits; one with a decimal point, a word or '' raises ValueError |