Python Exception Handling quiz (original) (raw)
What is an exception in Python?
How can you handle exceptions in Python?
- Using switch-case statements
What is the purpose of the finally block in exception handling?
- To define a block of code that will be executed if an exception occurs
- To specify the code to be executed regardless of whether an exception occurs or not
- To catch and handle specific exceptions
- To raise a custom exception
Which of the following statements is used to raise a custom exception in Python?
What will be the output of the following code?
try:
result = 10 / 0
except ZeroDivisionError:
result = "Infinity"
print(result)
What is the purpose of the else block in exception handling?
- To specify code that will be executed if no exceptions are raised
- To define custom exceptions
- To skip the execution of the block if an exception occurs
How can you catch multiple exceptions in a single except block?
- Using a comma-separated list of exceptions
- Using nested try-except blocks
- By defining a custom exception
What is the purpose of the assert statement in Python exception handling?
- To catch and handle exceptions
- To raise a custom exception
- To check if a given expression is true, otherwise raise an 'AssertionError'
What does the finally block execute if an exception is raised and not caught?
- It executes before the exception is raised
- It executes after the exception is raised
- It executes only if the exception is caught
What is the purpose of the with statement in Python exception handling?
- To create a new exception
- To simplify resource management using context managers
- To catch and handle exceptions
- To define a custom exception
There are 24 questions to complete.
Take a part in the ongoing discussion