PCEP Certification Practice: Tuples – Indexing, Slicing, Building, and Immutability
PCEP Certification Practice Test - Questions, Answers and Explanations
Here are 25 questions focusing on the topic "tuples: indexing, slicing, building, immutability" for the PCEP-30-02 certification exam. The questions include various formats such as single-select, multiple-select, gap fill, code insertion, sorting, and rearranging style questions. Each question is followed by the correct answer and an explanation.
Question 1: Which of the following correctly creates a tuple in Python?
- my_tuple = (1, 2, 3)
- my_tuple = [1, 2, 3]
- my_tuple = {1, 2, 3}
- my_tuple = 1, 2, 3
Answer: a) my_tuple = (1, 2, 3)
Explanation: A tuple is defined using parentheses (). The tuple can also be defined as 1, 2, 3 without parentheses, making d) also technically correct, but a) is the most explicit and clear way.
Question 2: What will be the output of the following code?
my_tuple = (1, 2, 3, 4) print(my_tuple[2])
- 1
- 2
- 3
- 4
Answer: c) 3
Explanation: Indexing in Python starts at 0, so my_tuple[2] accesses the third element, which is 3.
Question 3: Which of the following operations is not allowed on a tuple?
- Accessing elements by index
- Slicing the tuple
- Modifying an element
- Iterating over the tuple
Answer: c) Modifying an element
Explanation: Tuples are immutable, meaning their elements cannot be modified after they are created.
Question 4: Which of the following statements about tuples are true? (Choose all that apply)
- Tuples are immutable.
- Tuples can contain different data types.
- Tuples support indexing and slicing.
- Tuples are mutable and can be modified.
Answer: a) Tuples are immutable., b) Tuples can contain different data types., c) Tuples support indexing and slicing.
Explanation: Tuples are immutable, support indexing and slicing, and can hold elements of different data types. Option d) is incorrect because tuples are not mutable.
Question 5: Which of the following correctly slices a tuple my_tuple to get the elements (2, 3)? (Choose two)
- my_tuple[1:3]
- my_tuple[1:2]
- my_tuple[-3:-1]
- my_tuple[2:4]
Answer: a) my_tuple[1:3], c) my_tuple[-3:-1]
Explanation: Both a) and c) correctly slice the tuple to return (2, 3). But option c) depends on the length and content of the tuple.
Question 6: Tuples are __________, meaning their elements cannot be changed after they are created.
▼Answer: immutable
Explanation: The immutability of tuples means that once a tuple is created, its elements cannot be modified.
Question 7: The syntax for creating a tuple with a single element requires a __________ after the element.
▼Answer: comma
Explanation: A single-element tuple requires a trailing comma, for example, (element,) to be recognized as a tuple.
Question 8: Arrange the following code snippets in the correct order to create and slice a tuple:
- print(sliced_tuple)
- my_tuple = (10, 20, 30, 40, 50)
- sliced_tuple = my_tuple[1:4]
Answer:
- my_tuple = (10, 20, 30, 40, 50)
- print(sliced_tuple)
- sliced_tuple = my_tuple[1:4]
Explanation: The tuple is created first, then sliced, and finally, the result is printed.
Question 9: Arrange the following code snippets in the correct order to access the last element of a tuple:
- print(last_element)
- my_tuple = (100, 200, 300, 400)
- last_element = my_tuple[-1]
Answer:
- my_tuple = (100, 200, 300, 400)
- last_element = my_tuple[-1]
- print(last_element)
Explanation: The tuple is created first, then the last element is accessed using [-1], and finally, the result is printed.
Question 10: Complete the code to create a tuple containing the elements 5, 10, 15:
my_tuple = __________▼
Answer: (5, 10, 15)
Explanation: A tuple is created using parentheses and separating elements with commas.
Question 11: Complete the code to slice a tuple to get the elements (2, 4) from my_tuple:
sliced_tuple = my_tuple[__________]▼
Answer: 1:3
Explanation: The slice my_tuple[1:3] returns the elements (2, 4).
Question 12: Insert the correct code to create a tuple with a single element 5:
single_element_tuple = __________▼
Answer: (5,)
Explanation: A single-element tuple requires a trailing comma, so the correct syntax is (5,).
Question 13: Insert the correct code to access the second element of a tuple named data:
second_element = __________▼
Answer: data[1]
Explanation: The second element is accessed using the index [1] since indexing starts at 0.
Question 14: Rearrange the code to create a tuple with the elements 100, 200, 300 and print the first element:
- print(first_element)
- numbers = (100, 200, 300)
- first_element = numbers[0]
Answer:
- numbers = (100, 200, 300)
- first_element = numbers[0]
- print(first_element)
Explanation: The tuple is created first, the first element is accessed, and then the result is printed.
Question 15: Organize the steps to slice a tuple my_tuple to get the last two elements:
- sliced_tuple = my_tuple[-2:]
- print(sliced_tuple)
- my_tuple = (1, 2, 3, 4, 5)
Answer:
- my_tuple = (1, 2, 3, 4, 5)
- sliced_tuple = my_tuple[-2:]
- print(sliced_tuple)
Explanation: The tuple is defined first, the last two elements are sliced using [-2:], and then the result is printed.
Question 16: What does the following code output?
my_tuple = (10, 20, 30, 40) print(my_tuple[-3])
- 10
- 20
- 30
- 40
Answer: b) 20
Explanation: The index -3 starts counting from the end, so it accesses the second element, which is 20.
17. What does the following code output?
my_tuple = (5, 10, 15) my_tuple[1] = 20 print(my_tuple)
- (5, 20, 15)
- (5, 10, 15)
- Error
- (5, 10)
Answer: c) Error
Explanation: Since tuples are immutable, attempting to modify an element raises a TypeError.
Question 18: Tuples can be indexed and sliced just like lists.
- True
- False
Answer: a) True
Explanation: Tuples, like lists, support indexing and slicing operations.
Question 19: A tuple can be created without using parentheses.
- True
- False
Answer: a) True
Explanation: Parentheses are optional for creating a tuple, e.g., 1, 2, 3 is also a tuple.
Question 20: What happens if you try to slice a tuple with an out-of-range index?
- It raises an IndexError.
- It raises a ValueError.
- It returns an empty tuple.
- It returns None.
Answer: c) It returns an empty tuple.
Explanation: Slicing a tuple with an out-of-range index returns an empty tuple, not an error.
Question 21: Which of the following statements about tuple immutability is correct?
- Tuple elements cannot be modified, but they can be replaced.
- Tuple elements can be modified if they are mutable types.
- Tuples are fully immutable and cannot be changed after creation.
- Tuples can be modified if they contain only integers.
Answer: c) Tuples are fully immutable and cannot be changed after creation.
Explanation: Tuples are immutable, meaning their elements cannot be modified, added, or removed.
Question 22: Which of the following expressions correctly slices a tuple my_tuple to get all elements except the first one?
- my_tuple[1:]
- my_tuple[:-1]
- my_tuple[0:]
- my_tuple[:1]
Answer: a) my_tuple[1:]
Explanation: The slice my_tuple[1:] excludes the first element and returns all remaining elements.
Question 23: Indexing a tuple returns the element at the specified __________.
▼Answer: position
Explanation: Indexing returns the element at the specified position in the tuple.
Question 24: Tuples are often used to store __________ data that should not be changed.
▼Answer: related
Explanation: Tuples are used to store related data that should remain constant and unmodified.
Question 25: What is the output of the following code?
numbers = (1, 2, 3, 4, 5) print(numbers[2:4])
- (2, 3)
- (3, 4)
- (4, 5)
- (1, 2, 3)
Answer: b) (3, 4)
Explanation: The slice numbers[2:4] returns the elements at indices 2 and 3, which are (3, 4).
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/data-collections-tuples-indexing-slicing-building-immutability.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics