wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Exceptions and Exception Handling Worksheet

Total questions: 20

Worksheet time: 10mins

Name
Class
Date
1.

What is the main purpose of Python exceptions?

a)

To detect syntax errors during parsing

b)

To handle errors that occur during program execution in syntactically correct code

c)

To automatically fix runtime bugs

d)

To improve code compilation speed

2.

How does exception handling primarily benefit a program?

a)

It speeds up execution

b)

It prevents crashes and improves user experience

c)

It replaces the need for debugging tools

d)

It only works with syntax errors

3.

Which of the following is NOT listed as a benefit of exception handling in the document?

a)

Prevents application crashes

b)

Improves user experience

c)

Allows graceful degradation

d)

Automatically corrects invalid input

4.

In the basic try-except example, what output is printed when dividing 100 by 4?

a)

Something went wrong! Please try again.

b)

100 divided by your number is: 25.0

c)

ZeroDivisionError: division by zero

d)

Invalid input! Please enter a number.

5.

What happens in a try-except block when dividing 100 by 0 using a general except clause?

a)

The program crashes with a traceback

b)

It prints "Something went wrong! Please try again."

c)

It skips the except block entirely

d)

It raises a SyntaxError

6.

Why is catching specific exceptions (like ValueError) recommended over a general except?

a)

It catches fewer errors

b)

It provides meaningful messages and improves code clarity

c)

It only works for ZeroDivisionError

d)

It makes the code longer without benefits

7.

In the specific exception catching example, what message is displayed if the user enters "abc"?

a)

You can't divide by zero!

b)

That's not a valid number!

c)

100 divided by your number is: 100.0

d)

Calculation successful.

8.

What is the role of the else clause in a try-except-else block?

a)

It runs only if an exception occurs

b)

It executes if no exception occurs in the try block

c)

It always runs, like finally

d)

It handles syntax errors

9.

In the else clause example, what output follows successful input of 4?

a)

Division by zero is not allowed.

b)

Invalid input! Please enter a number.

c)

Calculation successful. Result: 25.0

d)

Program execution completed.

10.

When does the finally clause execute?

a)

Only if no exception occurs

b)

Only if an exception is caught

c)

Regardless of whether an exception occurred

d)

Never, unless explicitly called

11.

In the finally clause example with input 0, which message is always printed last?

a)

Result: infinite

b)

Error: Division by zero is not allowed.

c)

Program execution completed.

d)

Error: Please enter a valid number.

12.

What keyword is used to manually signal an error condition, like invalid age?

a)

try

b)

except

c)

raise

d)

else

13.

In the raising exceptions example, what message is printed for age input 125?

a)

Age cannot be negative

b)

Processing age: 125

c)

Invalid age: Are you sure this age is correct?

d)

Division by zero is not allowed.

14.

How is a custom exception class typically defined in Python?

a)

class MyError: pass

b)

class MyError(Exception): pass

c)

def MyError(): raise ValueError

d)

except MyError: print("Error")

15.

In the custom exceptions example, what exception is raised when withdrawing 150from150 from 100?

a)

ValueError

b)

ZeroDivisionError

c)

InsufficientFundsError with a specific message

d)

SyntaxError

16.

What does exception chaining (using raise ... from ...) primarily help with?

a)

Ignoring the original error

b)

Preserving the original error cause for debugging

c)

Catching multiple exceptions at once

d)

Automatically fixing chained errors

17.

In the exception chaining example, what is printed as the "Original cause" for int("abc")?

a)

Conversion failed

b)

invalid literal for int() with base 10: 'abc'

c)

Division by zero

d)

Age cannot be negative

18.

Which syntax catches multiple exceptions in one block?

a)

except ValueError or ZeroDivisionError:

b)

except (ValueError, ZeroDivisionError):

c)

except ValueError, ZeroDivisionError:

d)

except multiple [ValueError, ZeroDivisionError]:

19.

What are context managers (using with) commonly used for in exception handling?

a)

Raising custom exceptions

b)

Automatically managing resources like files, ensuring cleanup

c)

Only catching syntax errors

d)

Replacing try-except blocks entirely

20.

Which of the following is a best practice for exception handling from the document?

a)

Use empty except blocks for simplicity

b)

Catch specific exceptions instead of general ones

c)

Use exceptions for normal flow control

d)

Hide all errors without documentation