Free Practice Quiz Question List

8 Exceptions and Error Handling Online Quiz Questions

Use this free practice quiz with 20 questions to review 8 Exceptions and Error Handling, test your knowledge, and prepare for your next test or exam.

20 questions
01
Choose one
1 point

Which statement correctly distinguishes a Python exception from a syntax error?

  1. A

    It occurs only when a program has no try block.

  2. B

    It occurs while syntactically valid code is executing.

  3. C

    It is always caused by a missing colon.

  4. D

    It can be fixed only by adding a finally block.

02
True or false
1 point

True or false: In Python, an else block attached to a try statement runs only if the try block completes without raising an exception.

  1. A

    True

  2. B

    False

03
Written response
1 point

Which built-in exception class is raised by int("hello")?

04
Fill in the blank
1 point

Complete the standard Python exception-handling structure: place the risky operation in a block and handle a matching exception in an clause.

05
Choose one
1 point

Which use best matches the purpose of a finally block in Python?

  1. A

    Code that should run only after the try block succeeds

  2. B

    Cleanup such as closing a resource or releasing a lock

  3. C

    A replacement for every specific except clause

  4. D

    Code that prevents the try block from executing

06
Choose all
1 point

Select all practices that support robust exception handling.

  1. A

    Handle the most specific expected exception types.

  2. B

    Use a bare except for every operation so no error can escape.

  3. C

    Catch an exception only when the function can take a useful action.

  4. D

    Put unrelated application logic inside the try block to reduce code length.

07
True or false
1 point

Inside an except block, a bare raise re-raises the current exception while preserving its traceback.

  1. A

    True

  2. B

    False

08
Written response
1 point

What single Python keyword, used without an exception argument, re-raises the current exception from inside an except block while preserving its traceback?

09
Fill in the blank
1 point

User-defined application exceptions should normally inherit from , not directly from .

10
Choose one
1 point

A function catches an OSError while reading configuration and wants to expose a RuntimeError while preserving the original cause. Which code pattern does this?

  1. A

    raise RuntimeError(...) with no reference to the original error

  2. B

    raise RuntimeError(...) from error

  3. C

    return RuntimeError(...) from the handler

  4. D

    finally RuntimeError(...) after the file operation

11
Choose all
1 point

Select all choices that follow the material's guidance for preserving state and giving useful error messages in robust programs.

  1. A

    Validate incoming data at a program boundary.

  2. B

    Keep the try block limited to the operation expected to fail.

  3. C

    Include passwords and other sensitive details in error messages for debugging.

  4. D

    Give users a safe message that explains what failed and, when possible, how to fix it.

12
Open ended
1 point

Explain how the read_first_lines design in the material demonstrates robust exception handling. Include how it validates input, limits the risky operation, distinguishes expected failures, preserves diagnostic information, and separates successful processing from error handling.

13
Choose one
1 point

Which design best follows the guidance to keep risky operations narrow when converting user text to an integer and then using the result?

  1. A

    try: number = int(text) followed by an except ValueError for the conversion

  2. B

    Putting conversion, multiplication, printing, and unrelated logic in one broad try block

  3. C

    Catching every exception and printing only Something went wrong

  4. D

    Using a bare except around the entire function

14
Choose one
1 point

Which situation is a runtime exception rather than a syntax error?

  1. A

    A missing colon in an if statement

  2. B

    Dividing by zero in syntactically valid code

  3. C

    A misspelled keyword that prevents parsing

  4. D

    An unterminated string literal

15
Choose one
1 point

A program executes value = int("abc"). Which exception should a specific handler catch for this failure?

  1. A

    TypeError

  2. B

    IndexError

  3. C

    ValueError

  4. D

    NameError

16
Choose one
1 point

A function opens a resource and must release it regardless of whether the operation succeeds. Which clause is specifically intended for this cleanup?

  1. A

    Only when the try block raises an exception

  2. B

    Only when no except clause matches

  3. C

    Only after the else block runs

  4. D

    Whether or not an exception occurs

17
Choose one
1 point

A withdraw function detects that the requested amount exceeds the balance and cannot produce a valid balance. What is the most appropriate way for the function to report this condition?

  1. A

    Return None for every invalid input

  2. B

    Raise an exception that the caller can handle

  3. C

    Use a bare except inside the function

  4. D

    Print an error and return the original input

18
True or false
1 point

True or false: Catching every exception with a broad handler is generally a good way to make a program robust because it prevents unexpected failures from propagating.

  1. A

    True

  2. B

    False

19
Written response
1 point

Inside an except block, which Python statement re-raises the current exception while preserving its traceback?

20
Written response
1 point

What Python syntax should a function use when translating a low-level exception into a higher-level exception while explicitly preserving the original cause?