w3resource

PCEP Certification Practice: KeyError

PCEP Certification Practice Test - Questions, Answers and Explanations

Following questions focus on KeyError, covering its causes, handling, and common usage patterns with dictionaries in Python. The questions test understanding of how KeyError works and how to avoid or handle it effectively in Python programs.

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

  1. Raised when trying to access a list index that is out of range.
  2. Raised when a non-existent key is accessed in a dictionary.
  3. Raised when an undefined variable is referenced.
  4. Raised when an invalid type is passed to a function.

Answer: B) Raised when a non-existent key is accessed in a dictionary.

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

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

  • Accessing a non-existent key in a dictionary.
  • Trying to delete a key that does not exist in a dictionary.
  • Accessing an out-of-range index in a list.
  • Trying to reference an undefined variable.

Answer: A) Accessing a non-existent key in a dictionary.
B) Trying to delete a key that does not exist in a dictionary.

Explanation: KeyError is raised when trying to access or delete a key in a dictionary that does not exist. Accessing an out-of-range index in a list raises an IndexError, and referencing an undefined variable raises a NameError.

Question 3: The _________ exception is raised when trying to access a key that does not exist in a Python dictionary.

  1. IndexError
  2. KeyError
  3. LookupError

Answer: B) KeyError

Explanation: KeyError is raised when a non-existent key is accessed in a dictionary.

Question 4: The __________ exception is raised when you attempt to access a key that is not present in a dictionary.

Answer: KeyError

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

Question 5: Fill in the blank to catch the KeyError when accessing a missing key in a dictionary:

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

Answer: except KeyError:

Explanation: This code raises a KeyError when trying to access the key "address", which does not exist in the dictionary.

Question 6: What will happen when you try to access a non-existent key in a Python dictionary?

  1. The program will return None.
  2. The program raises a KeyError.
  3. The program raises an IndexError.
  4. The program will print a default value.

Answer: B) The program raises a KeyError

Explanation: If a key does not exist in the dictionary, Python raises a KeyError.

Question 7: Insert the correct exception handling code to catch a KeyError when a dictionary key is missing:

my_dict = {"fruit": "apple", "color": "red"}
try:
    print(my_dict["price"])
except __________:
    print("The key does not exist!")

Answer: except KeyError:

Explanation: This code will raise a KeyError because the key "price" does not exist in the dictionary. The except KeyError block will catch the exception.

Question 8: Which of the following scenarios can raise a KeyError in Python? (Select all that apply.)

  1. Accessing a non-existent key in a dictionary.
  2. Using .pop() on a dictionary with a non-existent key and no default value.
  3. Accessing an out-of-range index in a tuple.
  4. Trying to delete a key that doesn’t exist in a dictionary.

Answer: A) Accessing a non-existent key in a dictionary.
B) Using .pop() on a dictionary with a non-existent key and no default value.
D) Trying to delete a key that doesn’t exist in a dictionary.

Explanation: A KeyError is raised when accessing or deleting a key that does not exist in a dictionary or when using .pop() without a default value for a missing key.

Question 9: The _________ exception is raised when using the get() method of a dictionary with a missing key and no default value is provided.

  • KeyError
  • IndexError
  • No exception is raised.

Answer: C) No exception is raised.

Explanation: The get() method of a dictionary does not raise a KeyError. Instead, it returns None if the key is missing and no default value is provided.

Question 10: When trying to access a key that does not exist in a dictionary using [], Python raises a __________ exception.

Answer: KeyError

Explanation: A KeyError is raised when trying to access a missing key in a dictionary using square brackets ([]).

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 the root of Python’s exception hierarchy, followed by LookupError, which is the base class for KeyError and IndexError.

Question 12: Which of the following are true about the KeyError exception in Python? (Select all that apply.)

  1. It is raised when a dictionary key is missing.
  2. It is a subclass of LookupError.
  3. It is raised when an index is out of range in a list.
  4. It is raised when using .get() on a dictionary with a missing key.

Answer: A) It is raised when a dictionary key is missing.
B) It is a subclass of LookupError.

Explanation: KeyError is raised when a key is missing in a dictionary and is a subclass of LookupError. Using .get() with a missing key does not raise an error.

Question 13: The abstract exception _________ is 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 like KeyError and IndexError, which occur when a lookup operation fails.

Question 14: Trying to access a missing key in a dictionary using square brackets ([]) raises a __________ exception.

Answer: KeyError

Explanation: A KeyError is raised when trying to access a missing key in a dictionary using square brackets.

Question 15: Fill in the blank to handle a KeyError raised when trying to delete a missing key from a dictionary:

my_dict = {"name": "Alice", "age": 25}
try:
    del my_dict["gender"]
except __________ as e:
    print("Key error:", e)

Answer: except KeyError as e:

Explanation: Attempting to delete a non-existent key ("gender") from a dictionary will raise a KeyError. The except block catches the exception.

Question 16: Which of the following dictionary methods can be used to safely access a key without raising a KeyError?

  1. pop()
  2. get()
  3. setdefault()
  4. del

Answer: B) get()

Explanation: The get() method allows safe access to dictionary keys and does not raise a KeyError. It returns None or a default value if the key is missing.

Question 17: Insert the correct code to safely access a key in a dictionary without raising a KeyError:

my_dict = {"fruit": "apple", "color": "red"}
# INSERT CODE HERE to access key "price" safely
print(value)

Answer: value = my_dict.get("price", "N/A")

Explanation: The get() method is used to access the key "price" safely. If the key does not exist, it returns the default value "N/A" instead of raising a KeyError.

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

  1. Accessing a missing key using [] in a dictionary.
  2. Using .pop() on a dictionary without providing a default value.
  3. Accessing an out-of-bounds index in a list.
  4. Trying to delete a key that does not exist in a dictionary.

Answer: A) Accessing a missing key using [] in a dictionary.
B) Using .pop() on a dictionary without providing a default value.
D) Trying to delete a key that does not exist in a dictionary.

Explanation: KeyError is raised when accessing or deleting a non-existent key in a dictionary, or when using .pop() on a missing key without a default value. An out-of-bounds index in a list raises an IndexError.

Question 19: In Python, the _________ exception is raised when trying to access a key in a dictionary that does not exist.

  1. KeyError
  2. IndexError
  3. TypeError

Answer: A) KeyError

Explanation: KeyError is raised when a non-existent key is accessed in a dictionary.

Question 20: Trying to access a key that does not exist in a dictionary raises a __________ exception.

Answer: KeyError

Explanation: A KeyError is raised when accessing a key that does not exist in a dictionary using square brackets.

Question 21: Sort the following exceptions in the correct order from the most general to the most specific.

  1. LookupError
  2. KeyError
  3. BaseException

Answer: C) BaseException
A) LookupError
B) KeyError

Explanation: BaseException is the root of Python’s exception hierarchy. LookupError is the base class for key/index lookup errors, and KeyError is a subclass of LookupError.

Question 22: Which of the following dictionary methods can prevent a KeyError when accessing a missing key? (Select all that apply.)

  1. get()
  2. pop()
  3. setdefault()
  4. []

Answer: A) get()
C) setdefault()

Explanation: The get() and setdefault() methods can be used to access dictionary keys without raising a KeyError. The pop() method and [] raise a KeyError if the key is missing.

Question 23: The exception _________ is raised when you attempt to access a key that does not exist in a dictionary using square brackets.

  1. KeyError
  2. IndexError
  3. LookupError

Answer: A) KeyError

Explanation: KeyError is raised when accessing a missing key in a dictionary using square brackets.

Question 24: Fill in the blank to catch a KeyError raised when trying to access a missing key in a dictionary:

my_dict = {"name": "John", "age": 28}
try:
    print(my_dict["gender"])
except __________:
    print("Key does not exist")

Answer: except KeyError:

Explanation: Attempting to access a missing key ("gender") in a dictionary will raise a KeyError, which is caught and handled by the except block.

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

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

Answer: B) KeyError

Explanation: KeyError is raised when trying 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://www.w3resource.com/python/certificate/functions-and-exceptions-keyerror.php