AmbifulWorksheets
Practice that never repeats
CBSE · Class 12 · Computer Science

Exception Handling in Python

Standard difficulty · 20 questions · 40 marks · Time: 42 minutes

Name: Date: Score: ____ / 40

Show your working for Sections B and C. Answers are on the last page — try every question first!

Section A — Choose the correct answer8 × 1 = 8 marks
1.
An error in the rules of writing Python (such as a missing colon) is a:
  1. logical error
  2. ValueError
  3. exception at run time
  4. syntax error
2.
Which exception does this code raise?
x = float('pi')
  1. ValueError
  2. NameError
  3. IOError
  4. TypeError
3.
What does this print? (Output lines are separated by commas.)
L = [40, 10, 88]
try:
    n = int('five')
    print(30 // n)
    print(L[n])
except ZeroDivisionError:
    print('Cannot divide')
except ValueError:
    print('Not a number')
except IndexError:
    print('Bad index')
else:
    print('All fine')
finally:
    print('Done')
  1. Not a number, Done
  2. Not a number
  3. 30, Bad index, Done
  4. 30, 0, All fine, Done
4.
What does this print? (Output lines are separated by commas.)
def check(n):
    try:
        return 46 // n
    except ZeroDivisionError:
        return 0
    finally:
        print('checked')

print(check(7))
  1. 6, checked
  2. checked, 0
  3. checked, 6
  4. 6
5.
What does this print? (Output lines are separated by commas.)
def setMarks(m):
    if m < 0 or m > 80:
        raise ValueError('Marks out of range')
    return m

try:
    print(setMarks(68))
    print(setMarks(99))
    print(setMarks(49))
except ValueError as e:
    print(e)
  1. 68, Marks out of range
  2. 68, Marks out of range, 49
  3. 68, 99, 49
  4. Marks out of range
6.
What does this print?
data = ['9.5', 'six', '39', 'n/a', '48', '27']
good = 0
bad = 0
for x in data:
    try:
        good = good + int(x)
    except ValueError:
        bad = bad + 1
print(good, bad)
  1. 115 3
  2. 114 2
  3. 114 3
  4. 114 4
7.
How many times is skip printed?
for d in [9, 0, 0, 11]:
    try:
        q = 99 // d
    except ZeroDivisionError:
        print('skip')
    else:
        print(q)
  1. 2
  2. 3
  3. 4
  4. 1
8.
Which error does Python report before running this code?
print('8 apples)
  1. TypeError
  2. NameError
  3. SyntaxError
  4. ValueError
Section B — Short answer8 × 2 = 16 marks
9.
What is the difference between a syntax error and an exception?
10.
Name the exception raised by this code.
L = [1, 2, 5]
print(L[6])
11.
What does this print? Write the lines in order, separated by commas.
L = [76, 72, 56]
try:
    n = int('8')
    print(45 // n)
    print(L[n])
except ZeroDivisionError:
    print('Cannot divide')
except ValueError:
    print('Not a number')
except IndexError:
    print('Bad index')
else:
    print('All fine')
finally:
    print('Done')
12.
What does this print? Give (a) the first line (b) the second.
def check(n):
    try:
        return 82 // n
    except ZeroDivisionError:
        return 0
    finally:
        print('checked')

print(check(9))
13.
What does this print? Give (a) the value of good (b) the value of bad.
data = ['70', '', 'six', '4', '37', '29']
good = 0
bad = 0
for x in data:
    try:
        good = good + int(x)
    except ValueError:
        bad = bad + 1
print(good, bad)
14.
Write the four lines this prints, separated by commas.
for d in [9, 3, 0, 0]:
    try:
        q = 74 // d
    except ZeroDivisionError:
        print('skip')
    else:
        print(q)
15.
Write the lines this prints, in order, separated by commas.
def setMarks(m):
    if m < 0 or m > 50:
        raise ValueError('Marks out of range')
    return m

try:
    print(setMarks(36))
    print(setMarks(51))
    print(setMarks(15))
except ValueError as e:
    print(e)
16.
Write the letters this prints, in order, separated by commas.
try:
    x = 2
    y = x / 0
    print('A')
except ZeroDivisionError:
    print('B')
else:
    print('E')
finally:
    print('C')
print('D')
Section C — Word problems4 × 4 = 16 marks
17.
Explain the try … except … else … finally statement with a program that divides two numbers typed by the user.
18.
Write the output of each program, one line under another.
(a)
L = [79, 20, 88]
try:
    n = int('0')
    print(45 // n)
    print(L[n])
except ZeroDivisionError:
    print('Cannot divide')
except ValueError:
    print('Not a number')
except IndexError:
    print('Bad index')
else:
    print('All fine')
finally:
    print('Done')
(b)
for d in [2, 0, 0, 6]:
    try:
        q = 35 // d
    except ZeroDivisionError:
        print('skip')
    else:
        print(q)
19.
(a) What does the first program print? (b) What does the second print? (c) When does a finally block run?
(i)
def check(n):
    try:
        return 94 // n
    except ZeroDivisionError:
        return 0
    finally:
        print('checked')

print(check(9))
(ii)
def setMarks(m):
    if m < 0 or m > 100:
        raise ValueError('Marks out of range')
    return m

try:
    print(setMarks(27))
    print(setMarks(116))
    print(setMarks(85))
except ValueError as e:
    print(e)
20.
(a) What does this program print? (b) Which items of data raise the exception? (c) What would happen if the try and except lines were removed?
data = ['', 'ab', '34', '5.5', '40']
good = 0
bad = 0
for x in data:
    try:
        good = good + int(x)
    except ValueError:
        bad = bad + 1
print(good, bad)
AmbifulWorksheets
Practice that never repeats
CBSE · Class 12 · Computer Science

Answer key

Exception Handling in Python · AW-12CS-EXH-2026-V1 · For parents — fold back or tear off before handing the sheet to your child.

QAnswerWorking
1(d) syntax error—
2(a) ValueErrorthe string cannot be read as a number
3(a) Not a number, Doneint() of a word raises ValueError
4(c) checked, 6finally runs before the function hands back its value
5(a) 68, Marks out of rangeonce raise runs, the rest of the try block is skipped and except prints the message
6(c) 114 3int() raises ValueError for a string with a decimal point, a word or an empty string
7(a) 2only d = 0 raises ZeroDivisionError
8(c) SyntaxErrorthe string is never closed
9A 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.—
10IndexErrorL has indices 0 to 2 only
115, Bad index, DoneL has only indices 0 to 2, so L[8] raises IndexError after the division prints
12(a) checked; (b) 9finally runs before the function hands back its value
13(a) 140; (b) 2int() accepts only whole-number strings: one with a decimal point, a word or '' raises ValueError
148, 24, skip, skipelse runs only when the division worked
1536, Marks out of rangeraise jumps straight to the matching except
16B, C, Dx / 0 raises ZeroDivisionError, so A and else are skipped; finally always runs; then the program carries on
17try: 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, 5n 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