Softenant
Technologies
Python Exception Handling MCQs (25) — Answers at the End

Python Exception Handling MCQs (25)

Python Training in Vizag (Softenant)

1) Basic try/except

try:
    int("x")
    print("A")
except ValueError:
    print("B")
A. A
B. B
C. AB
D. (no output)

2) else runs only if no exception

try:
    x = 1/1
except ZeroDivisionError:
    print("E")
else:
    print("OK")
A. E
B. OK
C. EO
D. (no output)

3) finally runs no matter what

def f():
    try:
        return 1
    finally:
        print("F")
print(f())
A. prints F then 1
B. prints 1 then F
C. F not printed
D. Error

4) Catching general vs specific

try:
    {}["x"]
except Exception:
    print("G")
except KeyError:
    print("K")
A. K
B. G
C. GK
D. (no output)

5) Multiple exceptions in one except

try:
    1/0
except (ZeroDivisionError, ValueError):
    print("H")
A. H
B. Error
C. (no output)
D. Prints type name

6) Except as alias

try:
    [][2]
except IndexError as e:
    print(type(e).__name__)
A. Error
B. IndexError
C. Exception
D. NameError

7) Re-raising the current exception

try:
    1/0
except ZeroDivisionError:
    raise
A. Silently handled
B. Re-raises ZeroDivisionError
C. Raises RuntimeError
D. Prints None

8) Raising a new exception

def f():
    raise ValueError("bad")
try:
    f()
except ValueError as e:
    print("X")
A. X
B. (no output)
Prints “bad”
D. Error

9) Exception chaining (explicit)

try:
    int("x")
except ValueError as e:
    raise RuntimeError("wrap") from e
A. Only RuntimeError
B. RuntimeError with cause ValueError
C. Suppresses chaining
D. Prints wrap

10) Suppressing context

try:
    1/0
except ZeroDivisionError:
    raise RuntimeError("R") from None
A. Shows chained ZeroDivisionError
B. RuntimeError without context
C. Prints R
D. (no exception)

11) Order matters

try:
    {}["k"]
except KeyError:
    print("K")
except LookupError:
    print("L")
A. L
B. K
C. KL
D. (no output)

12) Uncaught exception bubbles up

def a(): 1/0
def b(): a()
try:
    b()
except ValueError:
    print("V")
A. V
B. Program terminates with ZeroDivisionError
C. (no output)
D. Prints both

13) Custom exception

class MyErr(Exception): pass
try:
    raise MyErr("x")
except MyErr:
    print("M")
A. M
B. (no output)
C. NameError
D. Error

14) finally can override return

def f():
    try:
        return "A"
    finally:
        return "B"
print(f())
A. A
B. B
C. AB
D. Error

15) assert by default

try:
    assert 2 + 2 == 5, "nope"
except AssertionError as e:
    print(e.args[0])
A. nope
B. True
C. (no output)
D. NameError

16) Catch-all except

try:
    open("no_such_file.txt")
except Exception:
    print("C")
A. C
B. (no output)
C. NameError
D. Only catches ValueError

17) Bare except pitfalls

try:
    1/0
except:
    print("X")
A. Also catches KeyboardInterrupt/SystemExit
B. Equivalent to except Exception
C. SyntaxError
D. Never catches anything

18) with-statement and exceptions

class M:
    def __enter__(self): return 1
    def __exit__(self, exc_type, exc, tb): return True
with M():
    1/0
print("OK")
A. Raises ZeroDivisionError
B. Suppressed; prints OK
C. TypeError
D. No print

19) Accessing traceback

import traceback
try:
    {}["x"]
except KeyError:
    s = traceback.format_exc()
    print(isinstance(s, str))
A. True
B. False
C. KeyError
D. None

20) finally and exceptions

def f():
    try:
        1/0
    finally:
        print("T")
f()
A. Prints T, then propagates ZeroDivisionError
B. Prints T and swallows error
C. No print
D. Returns None

21) try/except/else/finally order

out = []
try:
    out.append("try")
except:
    out.append("except")
else:
    out.append("else")
finally:
    out.append("finally")
print(out)
A. [‘try’,’finally’]
B. [‘try’,’else’,’finally’]
C. [‘try’,’except’,’finally’]
D. [‘finally’]

22) Creating a meaningful custom error

class BadAge(ValueError): pass
try:
    raise BadAge("too small")
except ValueError:
    print("V")
A. V
B. (no output)
C. TypeError
D. NameError

23) except tuple vs list

try:
    open("x"); 1/0
except (FileNotFoundError, ZeroDivisionError):
    print("Y")
A. Y
B. TypeError
C. (no output)
D. Prints exception message

24) Returning from finally vs raising

def g():
    try:
        1/0
    except ZeroDivisionError:
        return "E"
    finally:
        return "F"
print(g())
A. E
B. F
C. Raises ZeroDivisionError
D. None

25) Good pattern: narrow except

try:
    d = {"a":1}; d["b"]
except KeyError:
    print("Narrow")
except Exception:
    print("Broad")
A. Broad
B. Narrow
C. Both
D. (no output)

Answer Key

1) B

2) B

3) A

4) B

5) A

6) B

7) B

8) A

9) B

10) B

11) B

12) B

13) A

14) B

15) A

16) A

17) A

18) B

19) A

20) A

21) B

22) A

23) A

24) B

25) B