w3resource

PCEP Certification Practice: Python Built-In BaseException Hierarchy

PCEP Certification Practice Test - Questions, Answers and Explanations

Below is a set of 25 questions for the Certified Entry-Level Python Programmer (PCEP) examination focusing on the subtopic "Python Built-In Exceptions Hierarchy: BaseException." 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 all built-in exceptions in Python?

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

Answer: B) BaseException

Explanation: BaseException is the topmost class in Python’s built-in exception hierarchy from which all other exceptions are derived.

Question 2: Which of the following exceptions directly inherit from BaseException? (Choose all that apply)

  • KeyboardInterrupt
  • SystemExit
  • ZeroDivisionError
  • GeneratorExit

Answer: A) KeyboardInterrupt
B) SystemExit
D) GeneratorExit

Explanation: KeyboardInterrupt, SystemExit, and GeneratorExit are direct subclasses of BaseException. ZeroDivisionError inherits from the Exception class, not directly from BaseException.

Question 3: Complete the code to raise a BaseException.

raise ______("This is a base exception.")

Answer: BaseException

Explanation: The raise statement is used to raise an exception, and here BaseException is explicitly raised.

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

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

Answer: A) Error occurred

Explanation: The except block catches the BaseException, and the exception message "Error occurred" is printed.

Question 5: Insert the correct code to catch and handle a BaseException.

try:
    raise BaseException("Something went wrong.")
except ______:
    print("Caught a BaseException!")

Answer: BaseException

Explanation: The except BaseException clause catches any exception that is a subclass of BaseException.

Question 6: Which of the following statements is true about BaseException in Python?

  1. It is used to handle all exceptions.
  2. It is the base class for all exceptions, including SystemExit and KeyboardInterrupt.
  3. It is commonly used to handle user-defined exceptions.
  4. It is a subclass of the Exception class.

Answer: B) It is the base class for all exceptions, including SystemExit and KeyboardInterrupt.

Explanation: BaseException is the root class for all exceptions, including SystemExit and KeyboardInterrupt. Exception is its direct subclass

Question 7: Which of the following are valid subclasses of BaseException? (Choose all that apply)

  1. SystemExit
  2. TypeError
  3. AssertionError
  4. MemoryError

Answer: A) SystemExit
B) TypeError
C) AssertionError
D) MemoryError

Explanation: All the listed exceptions (SystemExit, TypeError, AssertionError, and MemoryError) are subclasses of BaseException.

Question 8: Arrange the steps to correctly raise and catch a BaseException.

  1. Use raise to raise a BaseException.
  2. Use the try block to execute code that may raise an exception.
  3. Catch the exception with an except BaseException block.

Answer: 2, 1, 3

Explanation: First, set up the try block, then raise the BaseException, and finally catch the exception using the except BaseException block.

Question 9: Complete the code to print the exception message when a BaseException is raised.

try:
    raise BaseException("Error occurred!")
except BaseException as ______:
    print(______)

Answer: e, e

Explanation: The variable e is used to capture the exception object, and print(e) outputs the exception message.

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

try:
    raise BaseException("Critical failure!")
except BaseException:
    print("An error was caught.")
  1. Critical failure!
  2. An error was caught.
  3. BaseException: Critical failure!
  4. Error

Answer: B) An error was caught.

Explanation: The except BaseException block catches the raised exception and prints "An error was caught.".

Question 11: Insert the correct code to handle an exception and print its message.

try:
    raise BaseException("Fatal error")
except ______ as error:
    print(error)

Answer: BaseException

Explanation: The except BaseException as error block captures the exception object and prints its message.

Question 12: Which exception is not a direct subclass of BaseException?

  1. Exception
  2. SystemExit
  3. KeyboardInterrupt
  4. NameError

Answer: D) NameError

Explanation: NameError is a subclass of Exception, which is itself a subclass of BaseException.

Question 13: Which of the following are characteristics of BaseException? (Choose all that apply)

  1. It is the root of the exception hierarchy in Python.
  2. It should generally not be caught in normal code.
  3. It is used for handling standard exceptions like TypeError and ValueError.
  4. It has no subclasses.

Answer:A) It is the root of the exception hierarchy in Python.
B) It should generally not be caught in normal code.

Explanation: BaseException is the root of Python's exception hierarchy and is typically not caught in user code because it catches all exceptions, including critical ones like SystemExit and KeyboardInterrupt.

Question 14: Arrange the steps to handle an exception and print its type.

  1. Use try to execute code that may raise an exception.
  2. Raise an exception using raise.
  3. Catch the exception using except.
  4. Print the type of the exception.

Answer: 2, 1, 3, 4

Explanation: First, set up the try block, raise the exception, catch it using except, and finally print the type of the exception using type().

Question 15: Complete the code to print the type of the caught exception.

try:
    raise BaseException("Error")
except BaseException as e:
    print(______(e))

Answer: type

Explanation: The type() function is used to print the type of the exception object e.

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

try:
    raise BaseException("Fatal error")
except BaseException as e:
    print(type(e))
  1. <class 'BaseException'>
  2. <class 'Exception'>
  3. Fatal error
  4. Error

Answer: A) <class 'BaseException'>

Explanation: The type() function returns the class of the object, and since the raised exception is of type BaseException, the output is <class 'BaseException'>.

17. Insert the correct code to ensure that a BaseException is caught and its message is printed.

try:
    raise BaseException("Critical issue")
except ______:
    print("Exception caught!")

Answer: BaseException

Explanation: The except BaseException clause catches the raised BaseException, and "Exception caught!" is printed.

Question 18: What happens if a KeyboardInterrupt is raised and not caught in a Python program?

  1. The program continues execution.
  2. The program ignores the exception.
  3. The program terminates.
  4. The program restarts.

Answer: C) The program terminates.

Explanation: When a KeyboardInterrupt is raised (usually by pressing Ctrl+C), it terminates the program unless the exception is explicitly caught.

Question 19: Which of the following exceptions directly inherit from BaseException and typically cause program termination? (Choose all that apply)

  1. KeyboardInterrupt
  2. SystemExit
  3. TypeError
  4. GeneratorExit

Answer: A) KeyboardInterrupt
B) SystemExit
D) GeneratorExit

Explanation: KeyboardInterrupt, SystemExit, and GeneratorExit are direct subclasses of BaseException and may cause the program to terminate. TypeError is a subclass of Exception.

Question 20: Arrange the steps to raise and handle a KeyboardInterrupt exception.

  1. Raise a KeyboardInterrupt.
  2. Use a try block for code that may be interrupted.
  3. Use an except block to catch the exception.
  4. Print a message indicating that the program was interrupted.

Answer: 2, 1, 3, 4,

Explanation: First, set up the try block, raise a KeyboardInterrupt, catch it using except, and finally print a message.

Question 21: Complete the code to catch a KeyboardInterrupt and print a message.

try:
    raise KeyboardInterrupt("Program interrupted")
except ______:
    print("KeyboardInterrupt caught")

Answer: KeyboardInterrupt

Explanation: The except KeyboardInterrupt clause catches the KeyboardInterrupt and prints the message "KeyboardInterrupt caught".

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

try:
    raise SystemExit("Exit program")
except SystemExit as e:
    print("Caught:", e)
  1. Exit program
  2. Caught: Exit program
  3. SystemExit: Exit program
  4. Program continues

Answer: B) Caught: Exit program

Explanation: The except SystemExit block catches the SystemExit exception and prints the message "Caught: Exit program".

Question 23: Insert the correct code to catch a SystemExit exception.

try:
    raise SystemExit("Exiting now")
except ______ as e:
    print("Caught:", e)

Answer: SystemExit

Explanation: The except SystemExit clause is used to catch the SystemExit exception and print its message.

Question 24: What is the role of GeneratorExit in Python?

  1. It is raised when a generator function is manually closed.
  2. It is raised when a generator runs out of values to yield.
  3. It is used to stop the iteration of a loop.
  4. It is raised when a generator encounters an error.

Answer: A) It is raised when a generator function is manually closed.

Explanation: GeneratorExit is raised inside a generator when the close() method is called on it.

Question 25: Which of the following are valid reasons to raise BaseException or its subclasses in Python? (Choose all that apply)

  1. To signal critical system-level events like termination or interruption.
  2. To handle standard exceptions like ValueError.
  3. To exit a program using SystemExit.
  4. To handle custom exceptions in user code.

Answer: A) To signal critical system-level events like termination or interruption.
C) To exit a program using SystemExit.

Explanation: BaseException and its direct subclasses like SystemExit and KeyboardInterrupt are used for system-level events. Standard exceptions like ValueError and user-defined exceptions are typically handled using Exception, not BaseException.



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-base-exception.php