Indexing and Slicing Lists in Python PCEP Exam preparation
PCEP Certification Practice Test - Questions, Answers and Explanations
Here are 25 questions related to the subtopic of "indexing and slicing" using lists in Python, formatted according to the PCEP-30-0x examination style.
Question 1: What will the following code output?
my_list = [10, 20, 30, 40, 50] print(my_list[2])
- 10
- 20
- 30
- 40
Answer: c) 30
Explanation: List indexing in Python starts at 0. The element at index 2 is 30.
Question 2: Complete the code to access the first element of the list my_list = [5, 10, 15, 20]:
first_element = my_list[___]▼
Answer: first_element = my_list[0]
Explanation: The first element of a list is accessed using index 0.
Question 3: What will the following code output?
my_list = [1, 2, 3, 4, 5] print(my_list[-1])
- 1
- 5
- 4
- IndexError
Answer: b) 5
Explanation: Negative indexing in Python allows you to access elements from the end of the list. my_list[-1] gives the last element, which is 5.
Question 4: Which of the following will return the element 4 from the list my_list = [1, 2, 3, 4, 5]? (Select all that apply)
- my_list[3]
- my_list[-2]
- my_list[2]
- my_list[4]
Answer: a) my_list[3]
b) my_list[-2]
Explanation: my_list[3] and my_list[-2] both access the element 4. Index 3 corresponds to the fourth element, and index -2 accesses the second-to-last element.
Question 5: Fill in the blank to get the sublist [2, 3, 4] from my_list = [1, 2, 3, 4, 5]:
sub_list = my_list[1:___]▼
Answer: sub_list = my_list[1:4]
Explanation: Slicing syntax my_list[start:end] returns elements from index start to end-1. my_list[1:4] returns [2, 3, 4].
Question 6: What does the following code return?
my_list = [5, 10, 15, 20, 25] print(my_list[::2])
- [5, 10, 15]
- [10, 20]
- [5, 15, 25]
- [25, 20, 15]
Answer: c) [5, 15, 25]
Explanation: The slicing my_list[::2] returns every second element from the list, starting with the first element, giving [5, 15, 25].
Question 7: Complete the code to reverse the list my_list = [1, 2, 3, 4, 5] using slicing:
reversed_list = my_list[___]▼
Answer: reversed_list = my_list[::-1]
Explanation: The slice my_list[::-1] reverses the list by starting from the end and stepping backwards.
Question 8: What will be the output of the following code?
my_list = [2, 4, 6, 8, 10] print(my_list[1:4])
- [2, 4, 6, 8]
- [4, 6, 8]
- [4, 6]
- [6, 8, 10]
Answer: b) [4, 6, 8]
Explanation: The slice my_list[1:4] returns elements from index 1 to 3, which are [4, 6, 8].
Question 9: Insert the correct code to access the second-to-last element of the list my_list = [10, 20, 30, 40, 50].
▼Answer: second_last_element = my_list[-2]
Explanation: Negative indexing allows accessing elements from the end of the list. my_list[-2] refers to the second-to-last element.
Question 10: Which of the following slices will result in an empty list? (Select all that apply)
- my_list[4:2]
- my_list[2:2]
- my_list[1:1]
- my_list[-1:-2]
Answer:
- my_list[2:2]
- my_list[4:2]
- my_list[-1:-2]
- my_list[1:1]
Explanation: All these slices return an empty list because the start index is either equal to or greater than the end index.
Question 11: Reorder the following operations to correctly slice the list my_list = [10, 20, 30, 40, 50] and get the result [20, 30, 40].
- my_list[4]
- my_list[1:4]
- my_list[:4]
Answer: b) my_list[1:4]
Explanation: The slice my_list[1:4] gives the sublist [20, 30, 40] by including elements from index 1 to 3.
Question 12: Fill in the blank to access the first three elements of my_list = [7, 14, 21, 28, 35]:
sub_list = my_list[___:3]▼
Answer: sub_list = my_list[:3]
Explanation: Omitting the start index in slicing starts from the beginning. my_list[:3] gives [7, 14, 21].
Question 13: Insert the correct code to slice the list my_list = [10, 20, 30, 40, 50] and get every second element starting from the second element.
▼Answer: sliced_list = my_list[1::2]
Explanation: The slice my_list[1::2] starts from index 1 and steps by 2, returning every second element [20, 40].
Question 14: What will the following code output?
my_list = [3, 6, 9, 12, 15] print(my_list[-3])
- 9
- 12
- 15
- 6
Answer: a) 9
Explanation: Negative index -3 refers to the third element from the end, which is 9.
Question 15: Which of the following will correctly slice the list my_list = [5, 10, 15, 20, 25] to get the sublist [10, 15, 20]? (Select all that apply)
- my_list[1:4]
- my_list[-4:-1]
- my_list[1:-1]
- my_list[2:4]
Answer: a) my_list[1:4]
b) my_list[-4:-1]
c) my_list[1:-1]
Explanation: These slices correctly extract the sublist [10, 15, 20] from the original list.
Question 16: Complete the code to access the middle three elements of my_list = [11, 22, 33, 44, 55, 66, 77]:
sub_list = my_list[___:___]▼
Answer: sub_list = my_list[2:5]
Explanation: The slice my_list[2:5] returns [33, 44, 55], which are the middle three elements of the list.
Question 17: Which slicing operation will return a new list that is a copy of the original list my_list = [1, 2, 3, 4, 5]?
- my_list[:]
- my_list[0:]
- my_list[::-1]
- my_list[:0]
Answer: a) my_list[:]
Explanation: Slicing with my_list[:] returns a new list that is a copy of the entire original list.
Question 18: Sort the following operations to access the last three elements of the list my_list = [10, 20, 30, 40, 50, 60].
- my_list[-3:]
- my_list[-3:6]
- my_list[3:]
Answer: a) my_list[-3:]
Explanation: The slice my_list[-3:] returns the last three elements [40, 50, 60].
Question 19: Complete the code to replace the first two elements of my_list = [1, 2, 3, 4, 5] with [10, 20]:
my_list[___:___] = [10, 20]▼
Answer: my_list[0:2] = [10, 20]
Explanation: The slice my_list[0:2] targets the first two elements, allowing them to be replaced by [10, 20].
Question 20: What will be the result of the following code?
my_list = [100, 200, 300, 400] print(my_list[-1:-4:-1])
- [400, 300, 200]
- [400, 300, 200, 100]
- [300, 200, 100]
- [200, 300, 400]
Answer: a) [400, 300, 200]
Explanation: The slice my_list[-1:-4:-1] starts at index -1 (400) and moves backwards by 1 step until it reaches -4.
Question 21: Fill in the blank to access every third element of my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]:
sub_list = my_list[___]▼
Answer: sub_list = my_list[::3]
Explanation: The slice my_list[::3] returns every third element [1, 4, 7].
Question 22: Insert the correct code to remove the last element of the list my_list = [1, 2, 3, 4, 5] without using the pop() method.
▼Answer: my_list = my_list[:-1]
Explanation: The slice my_list[:-1] excludes the last element and can be reassigned to the original list.
Question 23: What will the following code output?
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] print(my_list[7:3:-1])
- [8, 7, 6, 5]
- [8, 7, 6, 5, 4]
- [7, 6, 5, 4]
- [6, 5, 4]
Answer: a) [8, 7, 6, 5]
Explanation: The slice my_list[7:3:-1] starts at index 7 and moves backward, ending just before index 3.
Question 24: Complete the code to extract the elements [3, 4, 5] from my_list = [0, 1, 2, 3, 4, 5, 6]:
sub_list = my_list[___:___]▼
Answer: sub_list = my_list[3:6]
Explanation: The slice my_list[3:6] returns the sublist [3, 4, 5].
Question 25: Reorder the following steps to access elements from the third to the last element of the list my_list = [1, 2, 3, 4, 5, 6, 7]:
- my_list[2:]
- my_list[-3:]
- my_list[:-2]
Answer: a) my_list[2:]
Explanation: The slice my_list[2:] accesses all elements from the third element onward, resulting in [3, 4, 5, 6, 7].
Test your Python skills with w3resource's quiz
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics