Python Exception Handling Quiz

Python
0 Passed
0% acceptance

A 35-question quiz covering try-except blocks, handling multiple errors, using else and finally, and creating custom exceptions.

35 Questions
~70 minutes
1

Question 1

What is the primary purpose of a `try-except` block?

A
To handle runtime errors and prevent the program from crashing.
B
To speed up code execution.
C
To define a new function.
D
To test code syntax.
2

Question 2

Analyze the code below. What is the output?

python
try:
    print(1 / 0)
except ZeroDivisionError:
    print("Caught")
A
Caught
B
Infinity
C
0
D
Program crash
3

Question 3

Which block is executed ONLY if NO exception is raised in the `try` block?

A
else
B
finally
C
except
D
done
4

Question 4

Which block is executed ALWAYS, regardless of whether an exception occurred or not?

A
finally
B
else
C
always
D
cleanup
5

Question 5

Analyze the code below. What happens?

python
try:
    x = int("hello")
except ValueError:
    print("Value Error")
except TypeError:
    print("Type Error")
A
Prints 'Value Error'
B
Prints 'Type Error'
C
Prints both
D
Crashes
6

Question 6

How do you catch multiple exceptions in a single `except` line?

A
except (TypeError, ValueError):
B
except TypeError, ValueError:
C
except TypeError or ValueError:
D
except [TypeError, ValueError]:
7

Question 7

What keyword is used to manually trigger an exception?

A
raise
B
throw
C
trigger
D
error
8

Question 8

Analyze the code below. What is the output?

python
try:
    print("A")
    raise Exception("Error")
    print("B")
except:
    print("C")
A
A then C
B
A then B then C
C
A then Error
D
C
9

Question 9

Why is `except:` (bare except) generally discouraged?

A
It catches everything, including system exit signals (like Ctrl+C), making it hard to stop the program.
B
It is slower.
C
It is a syntax error in Python 3.
D
It only catches syntax errors.
10

Question 10

Which class should user-defined exceptions inherit from?

A
Exception
B
BaseException
C
Error
D
Object
11

Question 11

Analyze the code below. What is `e`?

python
try:
    1/0
except ZeroDivisionError as e:
    print(type(e))
A
<class 'ZeroDivisionError'>
B
<class 'Error'>
C
<class 'str'>
D
None
12

Question 12

What happens if an exception is raised inside a `finally` block?

A
The new exception is raised, potentially masking previous exceptions.
B
It is ignored.
C
The program crashes immediately.
D
It is handled by the previous except block.
13

Question 13

Analyze the code below. What is the output?

python
def func():
    try:
        return 1
    finally:
        return 2
print(func())
A
2
B
1
C
1 then 2
D
Error
14

Question 14

Which exception is raised when accessing a dictionary key that doesn't exist?

A
KeyError
B
ValueError
C
IndexError
D
LookupError
15

Question 15

Can you have a `try` block without an `except` block?

A
Yes, if you have a `finally` block.
B
No, `except` is mandatory.
C
Yes, always.
D
Only in Python 2.
16

Question 16

Analyze the code below. What is the output?

python
try:
    print("Start")
except:
    print("Error")
else:
    print("Success")
A
Start then Success
B
Start then Error
C
Start
D
Success
17

Question 17

What is the parent class of `ValueError` and `TypeError`?

A
Exception
B
StandardError
C
RunTimeError
D
BaseException
18

Question 18

How do you re-raise the active exception inside an `except` block?

A
raise
B
raise e
C
return
D
continue
19

Question 19

Analyze the code below. What is the output?

python
try:
    raise ValueError("Wrong")
except ValueError as e:
    print(e)
A
Wrong
B
ValueError
C
ValueError('Wrong')
D
Error
20

Question 20

Which exception is raised when an import fails?

A
ImportError (or ModuleNotFoundError)
B
LoadError
C
SystemError
D
ValueError
21

Question 21

What is the purpose of the `assert` statement?

A
To debug code by testing a condition; raises AssertionError if False.
B
To handle exceptions.
C
To define unit tests.
D
To stop the program normally.
22

Question 22

Analyze the code below. What is the output?

python
try:
    print("Try")
    exit()
finally:
    print("Finally")
A
Try then Finally
B
Try
C
Finally
D
Try then Error
23

Question 23

Can you define your own exception hierarchy?

A
Yes, by creating classes that inherit from each other.
B
No, exceptions are flat.
C
Only one level deep.
D
Only in Python 2.
24

Question 24

Analyze the code below. Which except block catches the error?

python
try:
    raise IndexError()
except LookupError:
    print("Lookup")
except IndexError:
    print("Index")
A
Lookup
B
Index
C
Both
D
None
25

Question 25

What is `sys.exc_info()` used for?

A
To get details about the current exception being handled.
B
To exit the system.
C
To get system info.
D
To raise an exception.
26

Question 26

Analyze the code below. What happens?

python
try:
    pass
except:
    print("Error")
else:
    print("Else")
finally:
    print("Finally")
A
Else then Finally
B
Error then Finally
C
Finally
D
Error
27

Question 27

Which exception is raised when a function receives an argument of the correct type but inappropriate value?

A
ValueError
B
TypeError
C
AttributeError
D
ArgumentError
28

Question 28

What is exception chaining (Python 3)?

A
When one exception causes another, Python tracks the cause using `from` (e.g., `raise NewError from OldError`).
B
Catching multiple exceptions.
C
Inheriting exceptions.
D
A list of errors.
29

Question 29

Analyze the code below. What is the output?

python
try:
    [][0]
except (ValueError, IndexError):
    print("Caught")
A
Caught
B
IndexError
C
ValueError
D
Crash
30

Question 30

Which exception is raised when you try to access an attribute that doesn't exist?

A
AttributeError
B
KeyError
C
NameError
D
AccessError
31

Question 31

Can you raise an instance of a class that does not inherit from BaseException?

A
No, TypeError will be raised.
B
Yes, any object can be raised.
C
Yes, but it won't be caught.
D
Only in Python 3.
32

Question 32

Analyze the code below. What is the output?

python
try:
    print("1")
except:
    print("2")
finally:
    print("3")
A
1 then 3
B
1 then 2 then 3
C
1
D
3
33

Question 33

What is the `args` attribute of an exception?

A
A tuple containing arguments passed to the exception constructor.
B
The error message string.
C
The traceback.
D
The line number.
34

Question 34

Analyze the code below. What happens?

python
try:
    x = 1/0
except ZeroDivisionError:
    pass
print("Done")
A
Prints 'Done'
B
Crashes
C
Prints nothing
D
Prints 'ZeroDivisionError'
35

Question 35

Why is it important to clean up resources in a `finally` block?

A
To prevent resource leaks (e.g., open file handles) even if errors occur.
B
To make the code faster.
C
It is required by syntax.
D
To save memory.

QUIZZES IN Python