Free Practice Quiz Question List

7 Exceptions and Error Handling Online Quiz Questions

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

20 questions
01
Choose one
1 point

What exception is raised by int("abc")?

  1. A

    TypeError

  2. B

    ValueError

  3. C

    NameError

  4. D

    IndexError

02
Choose one
1 point

In a Python try statement, when does the else clause execute?

  1. A

    It runs before the try block.

  2. B

    It runs whenever an exception is raised.

  3. C

    It runs only when the try block succeeds.

  4. D

    It always runs after finally.

03
Choose one
1 point

What is the primary purpose of raise ValueError("...") from error inside an except block?

  1. A

    It preserves the original exception as the cause of the new exception.

  2. B

    It suppresses all traceback information from the original exception.

  3. C

    It retries the failed operation automatically.

  4. D

    It converts every exception into BaseException.

04
Choose all
1 point

Which two practices follow the material's guidance for designing exception handlers? Select all correct choices.

  1. A

    Handle specific exception types whenever possible.

  2. B

    Place more specific handlers before more general handlers.

  3. C

    Use a bare except: for every recoverable error.

  4. D

    Put all application logic inside one large try block.

05
Choose all
1 point

Which two actions are consistent with the material's recommended validation process? Select all correct choices.

  1. A

    Define which values the contract accepts.

  2. B

    Allow invalid data to continue until a later operation fails.

  3. C

    Validate input where it enters a function or component.

  4. D

    Avoid stating the violated requirement to the caller.

06
True or false
1 point

True or false: A finally block executes even if the try block returns before reaching its end.

  1. A

    True

  2. B

    False

07
True or false
1 point

True or false: A bare except: is generally recommended for ordinary application logic because it handles only common runtime errors.

  1. A

    True

  2. B

    False

08
Written response
1 point

In the documented set_percentage(value) example, which Python exception class should be raised when value is less than 0 or greater than 100?

09
Written response
1 point

When reading a Python traceback from the bottom upward, what two pieces of information does the final line identify?

10
Fill in the blank
1 point

Complete both statements with the appropriate Python exception class. Raise when an input has an inappropriate type; raise when an input has an acceptable general type but an invalid value.

11
Fill in the blank
1 point

Complete the statement with the recommended keyword: open("notes.txt", encoding="utf-8") as file:

12
Open ended
1 point

Explain the three questions an effective error message should answer, and give a concise example that follows those principles.

13
Choose one
1 point

A try statement needs separate responses for FileNotFoundError and other Exception instances. Which handler order is correct?

  1. A

    except Exception: before except FileNotFoundError:

  2. B

    except FileNotFoundError: before except Exception:

  3. C

    except: before every typed handler

  4. D

    The order does not affect which handler runs

14
Choose one
1 point

A program converts user input inside a try block. The conversion succeeds, and the program should run code that depends on that success without placing that code inside the protected operation. Which clause should contain that code?

  1. A

    except

  2. B

    else

  3. C

    finally

  4. D

    raise

15
Choose one
1 point

A program attempts to open settings.txt, but that file does not exist. Which specific exception should normally be handled to report that the settings file was not found?

  1. A

    FileNotFoundError

  2. B

    PermissionError

  3. C

    ValueError

  4. D

    KeyError

16
Choose one
1 point

Which exception type is raised by the expression "total" + 3 because the operation receives an inappropriate type?

  1. A

    ValueError

  2. B

    NameError

  3. C

    TypeError

  4. D

    AssertionError

17
Choose one
1 point

A function requires its username argument to be a string. If the caller passes an integer, which exception should the function raise according to the validation guidance?

  1. A

    ValueError

  2. B

    TypeError

  3. C

    IndexError

  4. D

    OSError

18
True or false
1 point

True or false: If a try block executes a return statement, its finally block still executes before the function returns.

  1. A

    True

  2. B

    False

19
Written response
1 point

In the example create_user(username, age), what exception should be raised when username is a string containing only whitespace?

20
Written response
1 point

Which Python keyword begins the resource-management construct that automatically performs cleanup for a file when an exception occurs inside the block?