Python Exception Handling Quiz - MCQ Questions and Answers

Welcome to our "Python Exception Handling Quiz - MCQ Questions and Answers" blog post! This quiz is designed to test and enhance your understanding of exception handling in Python, a crucial aspect of writing robust and error-free code. Exception handling is an integral part of Python programming, ensuring that your programs gracefully handle errors and unexpected situations. 

In this quiz, you will encounter a series of multiple-choice questions that explore different facets of exception handling, including try-except blocks, built-in exceptions, custom exception creation, and best practices. 

Whether you're a beginner programmer learning the ropes or a seasoned developer seeking to solidify your exception-handling skills, these questions will challenge and educate you. Get ready to dive into the world of Python exceptions and emerge with a stronger grasp on handling them effectively!

1. What is an exception in Python?

a) An error that occurs at runtime
b) A syntax error
c) A warning
d) A type of function

Answer:

a) An error that occurs at runtime

Explanation:

An exception is an error that occurs during the execution of a program, disrupting its normal flow.

2. Which keyword is used for handling exceptions in Python?

a) handle
b) error
c) try
d) catch

Answer:

c) try

Explanation:

The 'try' keyword is used to catch and handle exceptions in Python.

3. How do you catch specific exceptions in Python?

a) try ... handle
b) try ... catch
c) try ... except
d) try ... error

Answer:

c) try ... except

Explanation:

Exceptions are caught using a 'try ... except' block.

4. What is the output of the following code?

   try:
       x = 1 / 0
   except ZeroDivisionError:
       print("Error occurred")
   else:
       print("No error")
a) Error occurred
b) No error
c) ZeroDivisionError
d) Nothing is printed

Answer:

a) Error occurred

Explanation:

The code catches the ZeroDivisionError, and prints "Error occurred".

5. Which block is executed even if an exception is not caught?

a) finally
b) else
c) except
d) try

Answer:

a) finally

Explanation:

The 'finally' block is executed regardless of whether an exception is caught or not.

6. How do you raise an exception manually in Python?

a) raise ExceptionName
b) throw ExceptionName
c) error ExceptionName
d) except ExceptionName

Answer:

a) raise ExceptionName

Explanation:

The 'raise' keyword is used to raise an exception manually.

7. What is the purpose of the 'else' block in a try-except statement?

a) To execute code if no exceptions occur
b) To handle the exception
c) To execute code regardless of whether an exception occurs
d) To define the exception to be caught

Answer:

a) To execute code if no exceptions occur

Explanation:

The 'else' block is executed if the try block does not raise an exception.

8. Which exception is raised for invalid syntax?

a) SyntaxError
b) ValueError
c) TypeError
d) RuntimeError

Answer:

a) SyntaxError

Explanation:

SyntaxError is raised when the parser encounters a syntax error.

9. What is the correct way to handle multiple exceptions?

a) except (Exception1, Exception2)
b) except Exception1, Exception2
c) except [Exception1, Exception2]
d) except Exception1 or Exception2

Answer:

a) except (Exception1, Exception2)

Explanation:

Multiple exceptions can be caught using a tuple in the except block.

10. How do you access the error message in an exception?

a) except Exception as e: print(e)
b) except Exception: print(Exception.message)
c) except Exception: print(error)
d) except Exception as e: print(error.message)

Answer:

a) except Exception as e: print(e)

Explanation:

The error message can be accessed by catching the exception as a variable.

11. Which exception is raised when a referenced item does not exist?

a) KeyError
b) IndexError
c) FileNotFoundError
d) NameError

Answer:

a) KeyError

Explanation:

KeyError is raised when a dictionary key is not found.

12. What is the output of the following code?

try:
        print("Hello")
        raise ValueError
    except ValueError:
        print("ValueError raised")
    finally:
        print("Goodbye")
a) Hello ValueError raised
b) Hello ValueError raised Goodbye
c) Hello Goodbye
d) ValueError raised Goodbye

Answer:

b) Hello ValueError raised Goodbye

Explanation:

The code prints "Hello", raises and catches a ValueError, then the finally block prints "Goodbye".

13. Which of the following is not a built-in exception in Python?

a) IOError
b) ZeroDivisionError
c) FileNotFoundError
d) OutOfBoundsError

Answer:

d) OutOfBoundsError

Explanation:

OutOfBoundsError is not a built-in exception in Python.

14. What does the 'assert' statement do in Python?

a) It is used to define an exception class
b) It raises an exception if a condition is not true
c) It handles exceptions
d) It tests if an exception occurs

Answer:

b) It raises an exception if a condition is not true

Explanation:

The 'assert' statement raises an AssertionError if a specified condition is not true.

15. What type of exceptions are handled by the following code?

try:
        # code
    except:
        # handle exception

    if condition:
        raise ValueError("Invalid value")
a) Only specified exceptions
b) All exceptions
c) No exceptions
d) Only built-in exceptions

Answer:

b) All exceptions

Explanation:

Using 'except' without specifying an exception type catches all exceptions.

16. What does the following code do?

if condition:
        raise ValueError("Invalid value")
a) It handles a ValueError
b) It prints "Invalid value"
c) It raises a ValueError if condition is true
d) It catches an error message

Answer:

c) It raises a ValueError if condition is true

Explanation:

The raise statement is used to raise an exception if a condition is met.

17. Which exception is raised when trying to access a non-existing index of a list?

a) KeyError
b) IndexError
c) ValueError
d) TypeError

Answer:

b) IndexError

Explanation:

IndexError is raised when trying to access an index that is out of the range of a list.

18. What is the purpose of the 'pass' statement in exception handling?

a) To pass control to the next exception handler
b) To do nothing in an exception block
c) To pass an error message
d) To exit the program

Answer:

b) To do nothing in an exception block

Explanation:

The 'pass' statement is used as a placeholder for future code, and it does nothing when executed.

19. Which keyword is used to define your own exception in Python?

a) define
b) exception
c) class
d) error

Answer:

c) class

Explanation:

Custom exceptions are defined by creating a new class.

20. What is the output of the following code?

try:
        x = 10 / 0
    except ZeroDivisionError:
        print("Cannot divide by zero")
    except:
        print("Other error")
a) Cannot divide by zero
b) Other error
c) No output
d) Error

Answer:

a) Cannot divide by zero

Explanation:

The specific exception for dividing by zero is caught, so "Cannot divide by zero" is printed.

Comments