w3resource

PCEP Certification Practice: Python Abstract Exceptions

PCEP Certification Practice Test - Questions, Answers and Explanations

Below is a set of 25 PCEP-style questions, focusing specifically on abstract exceptions in Python’s built-in exceptions hierarchy. Abstract exceptions are exceptions that serve as base classes for other exceptions and are generally not raised directly.

Question 1: Which of the following is considered an abstract exception in Python?

  1. KeyboardInterrupt
  2. TypeError
  3. Exception
  4. ValueError

Answer: C) Exception

Explanation: Exception is an abstract exception that serves as the base class for all non-system-exiting exceptions. It is not typically raised directly but is inherited by many other exceptions like TypeError and ValueError.

Question 2: Which of the following exceptions are abstract exceptions in Python? (Select all that apply.)

  • BaseException
  • Exception
  • ValueError
  • SystemExit

Answer: A) BaseException
B) Exception

Explanation: BaseException and Exception are abstract exceptions and act as base classes for other exceptions. They are generally not raised directly. ValueError and SystemExit are concrete exceptions that can be raised.

Question 3: In Python, _________ is the highest-level abstract exception in the exception hierarchy, from which all exceptions inherit.

  1. Exception
  2. BaseException
  3. TypeError

Answer: B) BaseException

Explanation: BaseException is at the top of Python's built-in exception hierarchy and is the parent class for all exceptions. Most exceptions inherit from Exception, which itself inherits from BaseException.

Question 4: The __________ abstract exception is the base class for all standard errors that do not lead to interpreter termination.

Answer: Exception

Explanation: Exception is the base class for all standard errors, meaning it is an abstract exception from which many other exceptions derive, but it does not include exceptions like SystemExit or KeyboardInterrupt.

Question 5: Fill in the blanks in this code to handle an abstract exception and its subclasses:

try:
    raise ValueError("A value error occurred")
except __________ as e:
    print(isinstance(e, Exception))

Answer: except ValueError as e:

Explanation: This code raises a ValueError, which is a subclass of the abstract Exception. The isinstance check will return True because ValueError is part of the Exception hierarchy.

Question 6: Which abstract exception is higher in the Python exception hierarchy?

  1. BaseException
  2. Exception
  3. TypeError
  4. OSError

Answer: A) BaseException

Explanation: BaseException is the top-most abstract exception in Python. All other exceptions, including Exception, TypeError, and OSError, inherit from BaseException.

Question 7: Insert code to verify whether an exception class is an abstract exception by checking its relationship to the BaseException class:

try:
    raise KeyError("Error")
except __________:
    print(issubclass(KeyError, BaseException))

Answer: except KeyError:

Explanation: This code checks if KeyError is a subclass of the abstract BaseException, which it is. The output will be True because KeyError is part of the exception hierarchy rooted in BaseException.

Question 8: Which of the following abstract exceptions in Python cannot be raised directly by the user? (Select all that apply.)

  1. BaseException
  2. Exception
  3. SystemExit
  4. MemoryError

Answer: A) BaseException
D) MemoryError

Explanation: BaseException is abstract exception designed as base classes and are not typically raised directly. The MemoryError is raised by the Python interpreter itself when it runs out of memory. Users typically do not raise this exception directly; it occurs as a result of system limitations.

Question 9: The abstract exception _________ is a direct subclass of BaseException and serves as the base class for most exceptions in Python.

  • SystemExit
  • KeyboardInterrupt
  • Exception

Answer: C) Exception

Explanation: Exception is a subclass of BaseException and serves as the base class for nearly all built-in exceptions in Python except those related to interpreter control like SystemExit and KeyboardInterrupt.

Question 10: The abstract exception __________ is used as the base class for all built-in exceptions related to arithmetic, type, and value errors.

Answer: Exception

Explanation: Exception serves as the base class for all built-in exceptions that handle various types of runtime errors, including those related to arithmetic, type, and value issues.

Question 11: Arrange the following abstract exceptions from the highest level to the lowest level in the Python exception hierarchy.

  1. Exception
  2. BaseException
  3. ArithmeticError

Answer: B) BaseException
A) Exception
C) ArithmeticError

Explanation: BaseException is the top-most abstract exception, followed by Exception, which is a direct subclass of BaseException. ArithmeticError is a subclass of Exception and represents errors related to arithmetic operations.

Question 12: Which of the following are abstract exceptions in Python? (Select all that apply.)

  1. LookupError
  2. BaseException
  3. ArithmeticError
  4. TypeError

Answer: A) LookupError
B) BaseException
C) ArithmeticError

Explanation: BaseException, LookupError, and ArithmeticError are abstract exceptions used as base classes for more specific exceptions like KeyError, ZeroDivisionError, etc. TypeError is not abstract—it is a concrete exception.

Question 13: The class _________ is an abstract exception that is a direct subclass of Exception and serves as the base class for all lookup-related errors.

  1. KeyError
  2. LookupError
  3. IndexError

Answer: B) LookupError

Explanation: LookupError is an abstract exception that serves as the base class for more specific lookup-related exceptions, such as KeyError and IndexError.

Question 14: __________ is an abstract exception that serves as the base class for exceptions that occur during arithmetic operations.

Answer: ArithmeticError

Explanation: ArithmeticError is an abstract exception that is the base class for exceptions like ZeroDivisionError and OverflowError.

Question 15: Fill in the blanks in the following code to catch an abstract exception that covers arithmetic errors:

try:
    result = 10 / 0
except __________ as e:
    print("Caught an arithmetic error:", e)

Answer: except ArithmeticError as e:

Explanation: ArithmeticError is the abstract base class for all exceptions related to arithmetic operations, and it will catch the ZeroDivisionError raised in this case.

Question 16: What is the purpose of abstract exceptions like Exception and ArithmeticError in Python?

  1. They define common behavior for specific exceptions.
  2. They terminate the program directly when raised.
  3. They must be raised directly by the user.
  4. They are used to optimize program speed.

Answer: A) They define common behavior for specific exceptions.

Explanation: Abstract exceptions serve as base classes and define common behavior for more specific exceptions, providing structure to the exception hierarchy without being raised directly.

Question 17: Insert the correct code to catch an abstract exception that could be raised by both KeyError and IndexError:

try:
    # Some dictionary or list operation
except __________:
    print("A lookup error occurred.")

Answer: except LookupError:

Explanation: LookupError is an abstract exception that serves as the base class for KeyError and IndexError, making it possible to catch both exceptions with one except block.

Question 18: Which of the following exceptions are subclasses of the abstract exception ArithmeticError? (Select all that apply.)

  1. OverflowError
  2. ZeroDivisionError
  3. FloatingPointError
  4. TypeError

Answer: A) OverflowError
B) ZeroDivisionError
C) FloatingPointError

Explanation: OverflowError, ZeroDivisionError, and FloatingPointError are all subclasses of the abstract exception ArithmeticError, which handles errors related to arithmetic operations. TypeError is unrelated to arithmetic errors.

Question 19: In the exception hierarchy, _________ is the abstract base class for exceptions that occur during arithmetic operations, such as ZeroDivisionError or OverflowError.

  1. BaseException
  2. ArithmeticError
  3. ValueError

Answer: B) ArithmeticError

Explanation: ArithmeticError is the base class for all exceptions related to arithmetic operations, and specific exceptions like ZeroDivisionError and OverflowError inherit from it.

Question 20: The abstract exception __________ serves as the base class for all errors related to incorrect lookups in dictionaries or sequences.

Answer: LookupError

Explanation: LookupError is the base class for all lookup-related exceptions, including KeyError (for dictionary lookups) and IndexError (for sequence lookups).

Question 21: Sort the following abstract exceptions in the correct order from highest to lowest in the Python exception hierarchy.

  1. BaseException
  2. Exception
  3. ArithmeticError

Answer: A) BaseException
B) Exception
C) ArithmeticError

Explanation: BaseException is at the top of the hierarchy, followed by Exception, and then more specific abstract exceptions like ArithmeticError.

Question 22: Which of the following exceptions are direct subclasses of the abstract exception LookupError? (Select all that apply.)

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

Answer: A) IndexError
B) KeyError

Explanation: IndexError and KeyError are subclasses of the abstract LookupError exception. ValueError and TypeError are unrelated.

Question 23: The abstract exception _________ is the base class for all exceptions that occur when a specific element is not found in a sequence or dictionary.

  1. TypeError
  2. KeyError
  3. LookupError

Answer: C) LookupError

Explanation: LookupError serves as the base class for exceptions that occur when an element lookup fails, like in KeyError and IndexError.

Question 24: Fill in the blanks to catch an abstract exception that covers both KeyError and IndexError:

try:
    my_dict = {"a": 1}
    print(my_dict["b"])
except __________:
    print("A lookup error occurred.")

Answer: except LookupError:

Explanation: LookupError is the abstract base class that covers both KeyError and IndexError, so it can be used to catch these exceptions in a single except block.

Question 25: Which abstract exception is the base class for all errors related to incorrect element lookups in dictionaries and sequences?

  1. IndexError
  2. KeyError
  3. LookupError
  4. NameError

Answer: C) LookupError

Explanation: LookupError is the abstract exception that serves as the base class for exceptions raised when an invalid element lookup occurs in a sequence or dictionary, such as IndexError and KeyError.



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-abstract-exceptions.php