PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Easy
Question 24
Q268 - Error Handling
Which of the following is an example of a Python built-in concrete exception?
-
A
- LookupError
-
B
- BaseException
-
C
- ArithmeticError
-
D
- IndexError
Reveal correct answer
Correct answer: D
Explanation
Topics: IndexError LookupError ArithmeticError BaseException
Try it yourself:
- # IndexError:
- # e1 = [1, 2][23] # IndexError ...
- print(IndexError.__subclasses__()) # []
- # LookupError:
- # {1, 2}.remove(3) # KeyError
- print(issubclass(KeyError, LookupError)) # True
- print(LookupError.__subclasses__())
- # [<class 'IndexError'>, <class 'KeyError'>, ...]
- # ArithmeticError:
- # e2 = 10 / 0 # ZeroDivisionError ...
- print(issubclass(ZeroDivisionError, ArithmeticError)) # True
- print(ArithmeticError.__subclasses__())
- # [<class 'FloatingPointError'>, <class 'OverflowError'>,
- # <class 'ZeroDivisionError'>]
Explanation:
IndexError is the only built-in concrete exception here.
In the meaning that you can get an error message saying: IndexError ...
LookupError is not a built-in concrete exception.
For example, you would get a KeyError or an IndexError
ArithmeticError is also not a built-in concrete exception.
For example, you would get a ZeroDivisionError
BaseException is the base class for all built-in exceptions
what makes it the opposite of a built-in concrete exception.
https://docs.python.org/3/library/exceptions.html#exception-hierarchy
Q268 (Please refer to this number, if you want to write me about this question.)
A. LookupError is an abstract base class that serves as the parent class for exceptions that are raised when a key or index used to access a mapping or sequence is invalid. It is not a concrete exception itself but a base class for other exceptions.
B. BaseException is the base class for all built-in exceptions in Python. It is not a concrete exception itself but serves as the root of the exception hierarchy in Python.
C. ArithmeticError is an abstract base class for exceptions that are raised for arithmetic errors like division by zero or invalid operations. It is not a concrete exception but a base class for more specific arithmetic-related exceptions.
D. IndexError is a concrete exception in Python that is raised when trying to access an index that is out of range in a sequence like a list or a tuple. It is a specific type of exception that is directly related to index errors in Python.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
