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?
- BaseException
- Exception
- SyntaxError
- 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)
- invalid literal for int() with base 10: 'abc'
- abc
- An error occurred
- 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?
- ZeroDivisionError
- KeyboardInterrupt
- IndexError
- 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)
- Catching general exceptions to prevent program crashes.
- Raising it explicitly using the raise statement.
- Handling system-level events like SystemExit.
- 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.
- Raise a ValueError.
- Use a try block to execute code that may raise an exception.
- Catch the ValueError using an except block.
- 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)
- General error
- An error occurred
- BaseException: General error
- 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?
- To handle critical system errors like SystemExit.
- To catch all exceptions and prevent the program from crashing.
- To raise custom exceptions.
- 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)
- AttributeError
- SystemExit
- NameError
- 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.
- Access a list element that is out of range.
- Use a try block.
- Catch the IndexError using an except block.
- 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)
- Error: list index out of range
- Error: [1, 2, 3]
- Error: x[5]
- 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?
- TypeError
- ValueError
- IndexError
- KeyError
Answer: A) TypeError
Explanation: TypeError is raised when an operation or function is applied to an object of an inappropriate type.
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
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics