w3resource

PCEP Certification Practice: Python Built-In Exception Class

PCEP Certification Practice Test - Questions, Answers and Explanations

Below is a set of 18 questions for the Certified Entry-Level Python Programmer (PCEP) examination focusing on the subtopic "Python Built-In Exceptions Hierarchy: Exception." The questions use various formats, including single and multiple-select questions, fill-in-the-gap, code fill, code insertion, sorting, and more.

Question 1: What is the base class for most standard exceptions in Python?

  1. BaseException
  2. Exception
  3. SyntaxError
  4. RuntimeError

Answer: B) Exception

Explanation: The Exception class is the base class for all built-in, non-system-exiting exceptions in Python.

Question 2: Which of the following exceptions are subclasses of Exception? (Choose all that apply)

  • TypeError
  • KeyboardInterrupt
  • ValueError
  • IOError

Answer: A) TypeError
C) ValueError
D) IOError

Explanation: TypeError, ValueError, and IOError are all subclasses of Exception. KeyboardInterrupt is a subclass of BaseException, not Exception.

Question 3: Complete the code to catch a generic exception using the Exception class.

try:
    x = 1 / 0
except ______:
    print("An error occurred.")

Answer: Exception

Explanation: The except Exception clause catches most standard exceptions, including arithmetic errors like division by zero.

Question 4: What will be the output of the following code?

try:
    result = int("abc")
except Exception as e:
    print(e)
  1. invalid literal for int() with base 10: 'abc'
  2. abc
  3. An error occurred
  4. None

Answer: A) invalid literal for int() with base 10: 'abc'

Explanation: The int() function raises a ValueError when trying to convert a non-numeric string to an integer, and the error message is captured and printed.

Question 5: Insert the correct code to handle any exception and print its type.

try:
    x = 1 / 0
except ______ as e:
    print(type(e))

Answer: Exception

Explanation: The except Exception as e block catches any standard exception and prints its type using type(e).

Question 6: Which of the following exceptions is not a subclass of Exception?

  1. ZeroDivisionError
  2. KeyboardInterrupt
  3. IndexError
  4. AttributeError

Answer: B) KeyboardInterrupt

Explanation: KeyboardInterrupt is a direct subclass of BaseException, not Exception.

Question 7: Which of the following are valid uses of the Exception class in Python? (Choose all that apply)

  1. Catching general exceptions to prevent program crashes.
  2. Raising it explicitly using the raise statement.
  3. Handling system-level events like SystemExit.
  4. Using it as a base class for user-defined exceptions.

Answer: A) Catching general exceptions to prevent program crashes.
B) Raising it explicitly using the raise statement.
D) Using it as a base class for user-defined exceptions.

Explanation: Exception is commonly used to catch general errors and prevent crashes, raise exceptions explicitly, and serve as a base class for custom exceptions. System-level events like SystemExit should not be handled with Exception.

Question 8: Arrange the steps to correctly handle a ValueError and print an error message.

  1. Raise a ValueError.
  2. Use a try block to execute code that may raise an exception.
  3. Catch the ValueError using an except block.
  4. Print a custom error message.

Answer: 2, 1, 3, 4

Explanation: First, set up the try block, raise a ValueError, catch it in the except block, and print a custom error message.

Question 9: Complete the code to explicitly raise an Exception with a custom message.

raise ______("Something went wrong")

Answer: Exception

Explanation: The raise statement is used to raise an Exception, and the custom message "Something went wrong" is passed as an argument.

Question 10: What will be the output of the following code?

try:
    raise Exception("General error")
except Exception as e:
    print(e)
  1. General error
  2. An error occurred
  3. BaseException: General error
  4. None

Answer: A) General error

Explanation: The Exception is raised with the message "General error", which is then printed in the except block.

Question 11: Insert the correct code to catch and print the exception type for a ZeroDivisionError.

try:
    x = 1 / 0
except ______ as e:
    print(type(e))

Answer: Exception

Explanation: The except Exception as e block catches the ZeroDivisionError because ZeroDivisionError is a subclass of Exception.

Question 12: What is the purpose of catching exceptions using the Exception class?

  1. To handle critical system errors like SystemExit.
  2. To catch all exceptions and prevent the program from crashing.
  3. To raise custom exceptions.
  4. To ignore all exceptions silently.

Answer: B) To catch all exceptions and prevent the program from crashing.

Explanation: Catching exceptions using Exception allows handling most standard exceptions, preventing the program from crashing.

Question 13: Which of the following standard exceptions in Python are subclasses of Exception? (Choose all that apply)

  1. AttributeError
  2. SystemExit
  3. NameError
  4. KeyError

Answer:A) AttributeError
C) NameError
D) KeyError

Explanation: AttributeError, NameError, and KeyError are subclasses of Exception. SystemExit is a subclass of BaseException.

Question 14: Arrange the steps to handle an IndexError and print a message.

  1. Access a list element that is out of range.
  2. Use a try block.
  3. Catch the IndexError using an except block.
  4. Print a message indicating the error.

Answer: 2, 1, 3, 4

Explanation: First, set up the try block, trigger an IndexError by accessing an out-of-range element, catch it in the except block, and print an error message.

Question 15: Complete the code to print the exception message when a ValueError is raised.

try:
    int("abc")
except Exception as ______:
    print(______)

Answer: e, e

Explanation: The variable e captures the exception object, and print(e) outputs the exception message.

Question 16: What will be the output of the following code?

try:
    x = [1, 2, 3]
    print(x[5])
except Exception as e:
    print("Error:", e)
  1. Error: list index out of range
  2. Error: [1, 2, 3]
  3. Error: x[5]
  4. None

Answer: A) Error: list index out of range

Explanation: Accessing an invalid index raises an IndexError, and the error message is printed.

17. Insert the correct method to raise an Exception with a custom message and catch it.

try:
    raise ______("Custom error")
except Exception as e:
    print(e)

Answer: Exception

Explanation: The raise Exception("Custom error") statement raises an Exception, which is caught and printed.

Question 18: Which of the following is a subclass of Exception used for handling errors related to invalid data types?

  1. TypeError
  2. ValueError
  3. IndexError
  4. KeyError

Answer: A) TypeError

Explanation: TypeError is raised when an operation or function is applied to an object of an inappropriate type.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python/certificate/functions-and-exceptions-exception.php