Python Exception Handling quiz

Last Updated :
Discuss
Comments

Question 1

What is an exception in Python?

  • A syntax error

  • A runtime error

  • A logical error

  • A compile-time error

Question 2

How can you handle exceptions in Python?

  • Using if-else statements

  • Using try-except blocks

  • Using switch-case statements

  • Using for loops

Question 3

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
     

Question 4

Which of the following statements is used to raise a custom exception in Python?

  • throw

  • raise 

  • catch

  • except

Question 5

What will be the output of the following code?

try:
   result = 10 / 0
except ZeroDivisionError:
   result = "Infinity"

print(result)
 

  • 10

  • "Infinity"

  • ZeroDivisionError

  • None

Question 6

What is the purpose of the else block in exception handling?

  • To handle exceptions

  • 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

Question 7

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

  • Using the catch keyword

Question 8

 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'

  • To ignore exceptions

Question 9

What does the finally block execute if an exception is raised and not caught?

  • It doesn't execute

  • It executes before the exception is raised

  • It executes after the exception is raised

  • It executes only if the exception is caught

Question 10

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