w3resource

PCEP Certification Practice Test: LookupError Exceptions

PCEP Certification Practice Test - Questions, Answers and Explanations

Below are 25 PCEP-style questions focusing on “LookupError” from Python’s built-in exception hierarchy. These questions use various formats including single- and multiple-select, "Place" & "Gap fill", sorting, code fill, and code insertion.

Question 1: Which of the following best describes the LookupError exception in Python?

  1. It is raised when there is an issue accessing or finding a file.
  2. It is an abstract base class for exceptions raised when a key or index is not found.
  3. It is raised directly when a dictionary lookup fails.
  4. It is raised when an arithmetic operation results in an invalid number.

Answer: B) It is an abstract base class for exceptions raised when a key or index is not found.

Explanation: LookupError is an abstract base class for exceptions that occur when a key or index lookup fails, such as KeyError and IndexError.

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

  • KeyError
  • IndexError
  • TypeError
  • ValueError

Answer: A) KeyError
B) IndexError

Explanation: KeyError and IndexError are subclasses of LookupError, while TypeError and ValueError are unrelated to lookup operations.

Question 3: _________ is raised when a key is not found in a Python dictionary.

  1. IndexError
  2. KeyError
  3. LookupError

Answer: B) KeyError

Explanation: KeyError is raised when a key lookup fails in a dictionary. It is a subclass of LookupError.

Question 4: The __________ exception is raised when an index is out of range for a sequence (like a list or tuple).

Answer: IndexError

Explanation: IndexError is raised when attempting to access an index in a sequence that is outside the valid range.

Question 5: Fill in the blanks to catch an exception raised when an invalid key is accessed in a dictionary:

my_dict = {"name": "Alice", "age": 30}
try:
    print(my_dict["address"])
except __________:
    print("Key not found in dictionary")

Answer: KeyError:

Explanation: KeyError is raised when trying to access a dictionary key that doesn’t exist, and this code catches and handles that exception.

Question 6: Which of the following exceptions is not a subclass of LookupError?

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

Answer: C) MemoryError

Explanation: MemoryError is unrelated to lookups. KeyError and IndexError are direct subclasses of LookupError.

Question 7: Insert the correct code to handle a LookupError when an invalid index is accessed in a list:

my_list = [1, 2, 3]
try:
    print(my_list[10])
except __________:
    print("Lookup error occurred.")

Answer: except LookupError:

Explanation: LookupError is the base class for exceptions like IndexError and KeyError, and it will catch any lookup-related errors such as trying to access an invalid index in a list.

Question 8: Which of the following operations can raise a LookupError or one of its subclasses? (Select all that apply.)

  1. Accessing a nonexistent key in a dictionary.
  2. Accessing an out-of-range index in a list.
  3. Performing a division by zero.
  4. Searching for an item in a set that does not exist.

Answer: A) Accessing a nonexistent key in a dictionary.
B) Accessing an out-of-range index in a list.

Explanation: LookupError or one of its subclasses (KeyError, IndexError) will be raised when trying to access a nonexistent key in a dictionary or an invalid index in a sequence. Division by zero raises ZeroDivisionError, and searching for an item in a set that doesn’t exist does not raise an exception.

Question 9: The _________ exception is raised when an index is out of the range of a sequence.

  • KeyError
  • IndexError
  • LookupError

Answer: B) IndexError

Explanation: IndexError is raised when attempting to access an index that is out of range in a sequence, such as a list or tuple.

Question 10: The exception __________ is raised when an invalid key is used to access a Python dictionary.

Answer: KeyError

Explanation: KeyError is raised when a key that does not exist in a dictionary is accessed.

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

  1. KeyError
  2. LookupError
  3. BaseException

Answer: C) BaseException
B) LookupError
A) KeyError

Explanation: BaseException is at the top of the exception hierarchy. LookupError is the base class for lookup-related exceptions, and KeyError is a subclass of LookupError.

Question 12: Which of the following are true about LookupError? (Select all that apply.)

  1. It is an abstract exception class.
  2. It is raised directly when an index is out of range.
  3. KeyError is a subclass of LookupError.
  4. It is raised for both key and index lookup failures.

Answer: A) It is an abstract exception class.
C) KeyError is a subclass of LookupError.
D) It is raised for both key and index lookup failures.

Explanation: LookupError is an abstract base class and not raised directly. It is the base class for exceptions that occur during key or index lookup operations, such as KeyError and IndexError.

Question 13: The abstract exception _________ serves as the base class for exceptions raised during a failed key or index lookup.

  1. IndexError
  2. LookupError
  3. KeyError

Answer: B) LookupError

Explanation: LookupError is the base class for exceptions that arise when key or index lookups fail, such as KeyError and IndexError.

Question 14: When trying to access a non-existent index in a list, Python raises the __________ exception, which is a subclass of LookupError.

Answer: IndexError

Explanation: IndexError is a specific subclass of LookupError and is raised when trying to access an invalid index in a list or other sequence types.

Question 15: Fill in the blanks to handle an exception raised by an invalid key lookup in a dictionary:

try:
    data = {"name": "John", "age": 25}
    print(data["gender"])
except __________ as e:
    print("Key error occurred:", e)

Answer: except KeyError as e:

Explanation: This code will catch the KeyError raised by trying to access a non-existent key "gender" in the dictionary.

Question 16: What happens when you try to access an index that is out of range in a Python list?

  1. The program prints None.
  2. The program raises a KeyError.
  3. The program raises an IndexError.
  4. The program skips that index.

Answer: C) The program raises an IndexError

Explanation: When attempting to access an out-of-range index in a Python list, an IndexError is raised.

Question 17: Insert the correct exception handling code to catch a LookupError and print a message:

try:
    my_list = [10, 20, 30]
    print(my_list[5])
except __________:
    print("A lookup error occurred.")

Answer: except LookupError:

Explanation: Catching LookupError will handle any lookup-related exception, including IndexError, which is raised in this example when accessing an invalid index.

Question 18: Which of the following scenarios can raise an IndexError? (Select all that apply.)

  1. Accessing the 10th element in a list with 5 elements.
  2. Accessing a dictionary key that doesn’t exist.
  3. Slicing a string with an invalid range.
  4. Accessing an out-of-bounds index in a tuple.

Answer: A) Accessing the 10th element in a list with 5 elements.
D) Accessing an out-of-bounds index in a tuple.

Explanation: IndexError is raised when attempting to access an invalid index in a sequence, like a list or tuple. Accessing an invalid dictionary key raises a KeyError, and slicing a string with an invalid range does not raise an exception.

Question 19: In Python, the _________ exception is raised when a sequence index is out of range.

  1. LookupError
  2. IndexError
  3. KeyError

Answer: B) IndexError

Explanation: IndexError is raised when attempting to access an index that is out of range in a sequence like a list or tuple.

Question 20: The exception __________ is raised when a key lookup fails in a Python dictionary, and it is a subclass of LookupError.

Answer: KeyError

Explanation: KeyError is raised when a key is not found in a dictionary, and it is a subclass of the abstract LookupError exception.

Question 21: Sort the following exceptions from the highest level to the lowest level in Python’s exception hierarchy.

  1. IndexError
  2. LookupError
  3. BaseException

Answer: C) BaseException
B) LookupError
A) IndexError

Explanation: BaseException is the root of Python’s exception hierarchy. LookupError is a base class for lookup-related exceptions, and IndexError is a specific subclass of LookupError.

Question 22: Which of the following operations can raise a KeyError? (Select all that apply.)

  1. Accessing a non-existent key in a dictionary.
  2. Accessing an invalid index in a list.
  3. Trying to delete a non-existent key in a dictionary.
  4. Accessing an invalid index in a tuple.

Answer: A) Accessing a non-existent key in a dictionary.
C) Trying to delete a non-existent key in a dictionary.

Explanation: KeyError is raised when trying to access or delete a non-existent key in a dictionary. Invalid list or tuple indexing raises IndexError, not KeyError.

Question 23: The exception _________ is raised when trying to access a key that doesn’t exist in a dictionary.

  1. LookupError
  2. KeyError
  3. IndexError

Answer: B) KeyError

Explanation: KeyError is raised when a key is not found in a dictionary. It is a subclass of LookupError.

Question 24: Fill in the blanks to catch an IndexError when trying to access an out-of-range index in a list:

my_list = [1, 2, 3]
try:
    print(my_list[10])
except __________:
    print("Index out of range.")

Answer: except IndexError:

Explanation: IndexError is raised when trying to access an index that is out of the valid range for a list or sequence.

Question 25: Which exception is raised when trying to access a non-existent key in a Python dictionary?

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

Answer: B) KeyError

Explanation: KeyError is raised when attempting to access a key that does not exist in a Python dictionary.



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-lookuperror.php