Python Exception Handling quiz

Last Updated :
Discuss
Comments

Question 1

What is an exception in Python?

  • A

    A syntax error

  • B

    A runtime error

  • C

    A logical error

  • D

    A compile-time error

Question 2

How can you handle exceptions in Python?

  • A

    Using if-else statements

  • B

    Using try-except blocks

  • C

    Using switch-case statements

  • D

    Using for loops

Question 3

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

  • A

    To define a block of code that will be executed if an exception occurs

  • B

    To specify the code to be executed regardless of whether an exception occurs or not

  • C

    To catch and handle specific exceptions

  • D

    To raise a custom exception

Question 4

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

  • A

    throw

  • B

    raise 

  • C

    catch

  • D

    except

Question 5

What will be the output of the following code?

Python
try:
    res = 10 / 0
except ZeroDivisionError:
    res = "Infinity"
print(res)


  • A

    10

  • B

    "Infinity"

  • C

    ZeroDivisionError

  • D

    None

Question 6

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

  • A

    To handle exceptions

  • B

    To specify code that will be executed if no exceptions are raised

  • C

    To define custom exceptions

  • D

    To skip the execution of the block if an exception occurs

Question 7

How can you catch multiple exceptions in a single except block?

  • A

    Using a comma-separated list of exceptions

  • B

    Using nested try-except blocks

  • C

    By defining a custom exception

  • D

    Using the catch keyword

Question 8

 What is the purpose of the assert statement in Python exception handling?

  • A

    To catch and handle exceptions

  • B

    To raise a custom exception

  • C

    To check if a given expression is true, otherwise raise an 'AssertionError'

  • D

    To ignore exceptions

Question 9

Will the finally block execute if an exception occurs and is not handled by any except block?

  • A

    It doesn't execute

  • B

    It executes before the exception is raised

  • C

    It executes after try/except blocks

  • D

    It executes only if the exception is caught

Question 10

What is the purpose of the with statement in Python exception handling?

  • A

    To create a new exception

  • B

    To simplify resource management using context managers

  • C

    To catch and handle exceptions

  • D

    To define a custom exception

There are 16 questions to complete.

Take a part in the ongoing discussion