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?
- Exception
- BaseException
- SyntaxError
- 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)
- Error occurred
- BaseException: Error occurred
- An error occurred
- 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?
- It is used to handle all exceptions.
- It is the base class for all exceptions, including SystemExit and KeyboardInterrupt.
- It is commonly used to handle user-defined exceptions.
- 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)
- SystemExit
- TypeError
- AssertionError
- 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.
- Use raise to raise a BaseException.
- Use the try block to execute code that may raise an exception.
- 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.")
- Critical failure!
- An error was caught.
- BaseException: Critical failure!
- 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?
- Exception
- SystemExit
- KeyboardInterrupt
- 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)
- It is the root of the exception hierarchy in Python.
- It should generally not be caught in normal code.
- It is used for handling standard exceptions like TypeError and ValueError.
- 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.
- Use try to execute code that may raise an exception.
- Raise an exception using raise.
- Catch the exception using except.
- 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))
- <class 'BaseException'>
- <class 'Exception'>
- Fatal error
- 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?
- The program continues execution.
- The program ignores the exception.
- The program terminates.
- 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)
- KeyboardInterrupt
- SystemExit
- TypeError
- 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.
- Raise a KeyboardInterrupt.
- Use a try block for code that may be interrupted.
- Use an except block to catch the exception.
- 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)
- Exit program
- Caught: Exit program
- SystemExit: Exit program
- 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?
- It is raised when a generator function is manually closed.
- It is raised when a generator runs out of values to yield.
- It is used to stop the iteration of a loop.
- 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)
- To signal critical system-level events like termination or interruption.
- To handle standard exceptions like ValueError.
- To exit a program using SystemExit.
- 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.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics