PCAP Python Exceptions and Assertions Quiz

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Thames
T
Thames
Community Contributor
Quizzes Created: 11982 | Total Attempts: 187,298
| Attempts: 12 | Questions: 20 | Updated: Aug 16, 2026
Please wait...
Question 1 / 21
🏆 Rank #-- ▾
0 %
0/100
Score 0/100

1. Which statement is executed regardless of whether an exception occurs in a try block?

Explanation

A `finally` block runs after the `try` block whether it completes normally or raises an exception. It is commonly used for cleanup tasks, such as closing files or releasing resources, because its code executes before the program continues or the exception is propagated. By contrast, `except` handles exceptions, while `else` runs only when none occurs.

Submit
Please wait...
About This Quiz
PCAP Python Exceptions and Assertions Quiz - Quiz

This quiz evaluates your understanding of Python exception handling and assertions, core topics in the PCAP certification. You'll test your knowledge of try-except blocks, custom exceptions, assertion statements, and best practices for managing runtime errors. Mastering these concepts is essential for writing robust, error-tolerant Python code.

2.

What first name or nickname would you like us to use?

You may optionally provide this to label your report, leaderboard, or certificate.

2. What does the 'raise' keyword do in Python?

Explanation

The `raise` keyword explicitly triggers an exception in Python. It can raise a built-in or custom exception, causing normal execution to stop unless the exception is handled. For example, `raise ValueError("Invalid input")` raises a `ValueError` with the given message.

Submit

3. When you use 'assert' statement and the condition is False, what happens?

Explanation

When an `assert` statement evaluates its condition as false, Python stops normal execution at that point and raises an `AssertionError`. Assertions are used to check assumptions or conditions during development; if the condition is true, execution continues without interruption.

Submit

4. What is the correct syntax to catch multiple exception types in a single except block?

Explanation

Use a tuple of exception classes in the `except` clause to handle either exception with the same block. For example, `except (ValueError, TypeError):` catches both `ValueError` and `TypeError`.

Submit

5. How do you create a custom exception class in Python?

Explanation

Define a custom exception by creating a class that inherits from Python’s `Exception` class. The `pass` statement provides an empty class body, which is sufficient when no extra behavior is needed. Instances of `MyError` can then be raised and caught like other exceptions. Subclassing `Exception` is preferred over inheriting directly from `BaseException`.

Submit

6. What exception is raised when you divide by zero?

Explanation

In Python, attempting to divide a number by zero raises `ZeroDivisionError`. This exception indicates that the division operation is undefined; it is a specific type of arithmetic error.

Submit

7. What exception occurs when accessing a list index that doesn't exist?

Explanation

Accessing a list position that is outside its valid range raises an `IndexError`. For example, trying to read `items[5]` when the list has only three elements triggers this exception.

Submit

8. Which statement allows you to re-raise the current exception?

Explanation

A bare `raise` inside an exception handler re-raises the currently handled exception, preserving its original traceback. `raise Exception` creates and raises a new exception instead.

Submit

9. True or False: The finally block will not execute if an exception is raised and not caught.

Explanation

In languages that support `try/finally`, the `finally` block runs when control leaves the `try` block, whether an exception is caught or propagates uncaught. It is commonly used for cleanup, such as closing files or releasing resources. It may not run if execution is forcibly terminated, such as by a crash or abrupt process exit.

Submit

10. What is the base class for all built-in exceptions in Python?

Explanation

`BaseException` is the root class of Python’s built-in exception hierarchy. Most exceptions that programs commonly handle, such as `ValueError` and `TypeError`, inherit from `Exception`, which itself inherits from `BaseException`. Other direct subclasses include `SystemExit` and `KeyboardInterrupt`.

Submit

11. True or False: An except block can catch an exception without specifying an exception type.

Explanation

A bare `except:` block catches any exception raised within its associated `try` block, even when no exception type is named. Because it can also catch system-exiting exceptions, it’s generally better to specify the exception types you intend to handle.

Submit

12. True or False: Assertions should be used for checking user input validation in production code.

Explanation

Assertions are intended to detect programming errors and verify internal assumptions, not to validate potentially invalid user input. They may be disabled in production, so input validation should use explicit checks that reliably handle errors and provide appropriate feedback.

Submit

13. What exception occurs when you import a module that doesn't exist?

Explanation

When Python cannot locate a module during an import, it raises `ModuleNotFoundError`. This exception is a subclass of `ImportError`, so either name may be used depending on the Python version or how the import failure occurs. The other options describe different problems, such as missing files, undefined names, or invalid syntax.

Submit

14. What exception is raised when a function receives an argument of an incorrect type?

Explanation

A TypeError is raised when an operation or function receives an argument of an inappropriate type, such as passing a string where a number is expected.

Submit

15. What exception is raised when trying to access a dictionary key that doesn't exist?

Explanation

Accessing a dictionary with a key that isn’t present raises a `KeyError`, indicating that the requested key could not be found.

Submit

16. Can you have multiple except blocks after a single try block?

Explanation

A single try block can be followed by several except blocks, each designed to handle a different exception type or group of types. When an exception occurs, Python checks the handlers in order and runs the first compatible one. Typically, place specific exceptions before broader handlers; only one matching handler runs.

Submit

17. In the assert statement 'assert x > 0, "x must be positive"', what is the second argument?

Explanation

The second argument to `assert` is an optional message displayed when the condition fails. Here, `"x must be positive"` explains why the assertion failed, so it is a custom error message.

Submit

18. What is the order of execution for try-except-else-finally blocks?

Explanation

The `try` block runs first. If it raises a matching exception, the corresponding `except` block runs; otherwise, the `else` block runs. The `finally` block runs afterward in either case, whether or not an exception occurred.

Submit

19. The 'else' block in try-except executes when ____.

Explanation

In a try-except structure, the `else` block runs only when the code inside `try` finishes without raising an exception. If an exception occurs, control goes to a matching `except` block instead.

Submit

20. In exception handling, 'as' keyword is used to ____.

Explanation

The `as` keyword assigns the caught exception object to a variable, allowing the program to inspect the exception or use its details within the exception-handling block.

Submit
×
Saved
Thank you for your feedback!
View My Results
Cancel
  • All
    All (20)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Which statement is executed regardless of whether an exception occurs...
What does the 'raise' keyword do in Python?
When you use 'assert' statement and the condition is False, what...
What is the correct syntax to catch multiple exception types in a...
How do you create a custom exception class in Python?
What exception is raised when you divide by zero?
What exception occurs when accessing a list index that doesn't exist?
Which statement allows you to re-raise the current exception?
True or False: The finally block will not execute if an exception is...
What is the base class for all built-in exceptions in Python?
True or False: An except block can catch an exception without...
True or False: Assertions should be used for checking user input...
What exception occurs when you import a module that doesn't exist?
What exception is raised when a function receives an argument of an...
What exception is raised when trying to access a dictionary key that...
Can you have multiple except blocks after a single try block?
In the assert statement 'assert x > 0, "x must be positive"', what is...
What is the order of execution for try-except-else-finally blocks?
The 'else' block in try-except executes when ____.
In exception handling, 'as' keyword is used to ____.
play-Mute sad happy unanswered_answer up-hover down-hover success oval cancel Check box square blue
Alert!