PCEP Certification Practice: IndexError Questions & Explanations
PCEP Certification Practice Test - Questions, Answers and Explanations
Here are 25 PCEP-style questions focusing on IndexError, part of Python’s built-in exceptions hierarchy. The questions use various formats, including single and multiple-select questions, fill-in-the-gap, code fill, code insertion, sorting, and more.
Question 1: Which of the following best describes the IndexError exception in Python?
- It is raised when trying to access an element in a dictionary that does not exist.
- It is raised when trying to access an index that is out of range in a sequence.
- It is raised when a division by zero is attempted.
- It is raised when an undefined variable is used.
Answer: B) It is raised when trying to access an index that is out of range in a sequence.
Explanation: IndexError occurs when trying to access an invalid index in a sequence (like a list, tuple, or string).
Question 2: Which of the following operations can raise an IndexError in Python? (Select all that apply.)
- Accessing an out-of-range index in a list.
- Accessing an out-of-range index in a tuple.
- Accessing a non-existent key in a dictionary.
- Slicing a list with an invalid range.
Answer: A) Accessing an out-of-range index in a list.
B) Accessing an out-of-range index in a tuple.
Explanation: IndexError occurs when trying to access an invalid index in a list or tuple. Accessing a non-existent dictionary key raises a KeyError. Slicing lists does not raise an IndexError; it returns an empty list if the range is invalid.
Question 3: The _________ exception is raised when trying to access an index that is out of range for a Python list or tuple.
- KeyError
- IndexError
- TypeError
Answer: B) IndexError
Explanation: IndexError is raised when you try to access an index that does not exist in a sequence such as a list or tuple.
Question 4: The __________ exception is raised when you attempt to access an index that does not exist in a sequence.
▼Answer: IndexError
Explanation: IndexError is raised when you attempt to access an index outside the range of a sequence like a list, tuple, or string.
Question 5: Fill in the blank to catch an IndexError when accessing an invalid index in a list:
my_list = [1, 2, 3] try: print(my_list[5]) except __________: print("Index out of range")▼
Answer: except IndexError:
Explanation: IndexError is raised when trying to access an index that does not exist, such as index 5 in a list of length 3.
Question 6: What will happen when you try to access an invalid index in a Python tuple?
- The program will return None.
- The program raises an IndexError.
- The program raises a TypeError.
- The program will continue execution with no error.
Answer: B) The program raises an IndexError
Explanation: Accessing an invalid index in a tuple (or any sequence) raises an IndexError
Question 7: Insert the correct exception handling code to catch an IndexError when accessing an out-of-range index in the following code:
my_tuple = (1, 2, 3) try: print(my_tuple[4]) except __________: print("Invalid index!")▼
Answer: except IndexError:
Explanation: This code catches the IndexError exception, which is raised when trying to access index 4 in a tuple of length 3.
Question 8: Which of the following data structures can raise an IndexError when accessed with an invalid index? (Select all that apply.)
- List
- Tuple
- String
- Dictionary
Answer: A) List
B) Tuple
C) String
Explanation: IndexError can be raised when accessing an invalid index in sequences like lists, tuples, and strings. Dictionaries raise KeyError when a non-existent key is accessed.
Question 9: In Python, the _________ exception is raised when an invalid index is used to access a list.
- IndexError
- KeyError
- TypeError
Answer: A) IndexError
Explanation: IndexError is raised when an invalid index is used to access elements in a sequence like a list.
Question 10: Attempting to access index 5 in a list of length 3 will raise the __________ exception.
▼Answer: IndexError
Explanation: Trying to access an index beyond the valid range of a list, like index 5 in a list with only 3 elements, raises an IndexError.
Question 11: Arrange the following exceptions in the correct order, from the highest level to the lowest in the Python exception hierarchy.
- IndexError
- LookupError
- BaseException
Answer: C) BaseException
B) LookupError
A) IndexError
Explanation: BaseException is the highest-level exception. LookupError is the base class for exceptions related to lookup operations, and IndexError is a subclass of LookupError.
Question 12: Which of the following are true about the IndexError exception in Python? (Select all that apply.)
- It is raised when a sequence index is out of range.
- It is a subclass of LookupError.
- It is raised when a dictionary key is missing.
- It is a direct subclass of KeyError.
Answer: A) It is raised when a sequence index is out of range.
B) It is a subclass of LookupError.
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 _________ is the base class for exceptions raised during a failed key or index lookup, such as IndexError.
- IndexError
- LookupError
- TypeError
Answer: B) LookupError
Explanation: LookupError is the base class for exceptions raised during failed key or index lookups, including IndexError and KeyError.
Question 14: Trying to access an index in a list that is outside the valid range will raise an __________ exception.
▼Answer: IndexError
Explanation: An IndexError is raised when an index outside the valid range of a sequence like a list is accessed.
Question 15: Fill in the blank to handle an IndexError raised when trying to access an invalid index in a tuple:
my_tuple = (4, 5, 6) try: print(my_tuple[10]) except __________: print("Tuple index out of range")▼
Answer: except IndexError:
Explanation: IndexError is raised when accessing an index outside the valid range of a tuple, and the exception is caught in this code.
Question 16: Which of the following exceptions is raised when accessing an invalid index in a string?
- KeyError
- TypeError
- IndexError
- ValueError
Answer: C) IndexError
Explanation: IndexError is raised when accessing an index in a string that is outside the valid range.
Question 17: Insert the correct code to catch an IndexError when trying to access an invalid index in a list:
my_list = ['a', 'b', 'c'] try: # INSERT CODE HERE except IndexError: print("Invalid index!")▼
Answer: print(my_list[5])
Explanation: This code tries to access index 5 in a list with only 3 elements, which raises an IndexError. The except block catches this exception.
Question 18: Which of the following operations can raise an IndexError in Python? (Select all that apply.)
- Accessing index -1 in a list.
- Accessing index 100 in a list of length 10.
- Accessing index 5 in a tuple of length 2.
- Accessing an invalid slice of a list.
Answer: B) Accessing index 100 in a list of length 10.
C) Accessing index 5 in a tuple of length 2.
Explanation: Accessing an index larger than the length of a sequence raises an IndexError. Index -1 refers to the last element and does not raise an error, and slicing a list with an invalid range does not raise an exception.
Question 19: In Python, the _________ exception is raised when an invalid index is used to access a string.
- IndexError
- KeyError
- TypeError
Answer: A) IndexError
Explanation: IndexError is raised when an invalid index is used to access a string or other sequence types.
Question 20: When trying to access index 4 in a tuple with only 2 elements, Python raises the __________ exception.
▼Answer: IndexError
Explanation: An IndexError is raised when accessing an index that is outside the valid range of a sequence, such as a tuple.
Question 21: Sort the following exceptions in the correct order from the most general to the most specific.
- LookupError
- IndexError
- BaseException
Answer: C) BaseException
A) LookupError
B) IndexError
Explanation: BaseException is the most general, followed by LookupError, and then IndexError, which is a subclass of LookupError.
Question 22: Which of the following data types can raise an IndexError when accessed with an invalid index? (Select all that apply.)
- List
- String
- Tuple
- Set
Answer: A) List
B) String
C) Tuple
Explanation: IndexError occurs when an invalid index is used with sequences like lists, strings, and tuples. Sets do not have indices and therefore do not raise IndexError.
Question 23: The exception _________ is raised when you attempt to access an index outside the valid range of a list.
- IndexError
- KeyError
- TypeError
Answer: A) IndexError
Explanation: IndexError is raised when an index is accessed that is out of range for a list or any other sequence type.
Question 24: Fill in the blank to catch an IndexError when trying to access an invalid index in a string:
my_string = "Python" try: print(my_string[10]) except __________: print("String index out of range")▼
Answer: except IndexError:
Explanation: IndexError is raised when accessing an index outside the valid range of a string, such as index 10 in a string of length 6.
Question 25: What exception is raised when trying to access an invalid index in a Python list?
- KeyError
- IndexError
- TypeError
- ValueError
Answer: B) IndexError
Explanation: IndexError is raised when an invalid index is used to access a list or other sequence in Python.
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-indexerror.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics