# 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?

1. my_tuple = (1, 2, 3)
2. my_tuple = [1, 2, 3]
3. my_tuple = {1, 2, 3}
4. 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. 1
2. 2
3. 3
4. 4

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?

1. Accessing elements by index
2. Slicing the tuple
3. Modifying an element
4. Iterating over the tuple

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)

1. Tuples are immutable.
2. Tuples can contain different data types.
3. Tuples support indexing and slicing.
4. 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)

1. my_tuple[1:3]
2. my_tuple[1:2]
3. my_tuple[-3:-1]
4. my_tuple[2:4]

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.

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.

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:

1. print(sliced_tuple)
2. my_tuple = (10, 20, 30, 40, 50)
3. sliced_tuple = my_tuple[1:4]

1. my_tuple = (10, 20, 30, 40, 50)
2. print(sliced_tuple)
3. 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]

• 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 = __________
```

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[__________]
```

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 = __________
```

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 = __________
```

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]

• 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)

• 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])
```
1. 10
2. 20
3. 30
4. 40

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)
```
1. (5, 20, 15)
2. (5, 10, 15)
3. Error
4. (5, 10)

Explanation: Since tuples are immutable, attempting to modify an element raises a TypeError.

Question 18: 18. Tuples can be indexed and sliced just like lists.

1. True
2. False

Explanation: Tuples, like lists, support indexing and slicing operations.

Question 19: A tuple can be created without using parentheses.

1. True
2. False

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?

1. It raises an IndexError.
2. It raises a ValueError.
3. It returns an empty tuple.
4. 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?

1. Tuple elements cannot be modified, but they can be replaced.
2. Tuple elements can be modified if they are mutable types.
3. Tuples are fully immutable and cannot be changed after creation.
4. 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?

1. my_tuple[1:]
2. my_tuple[:-1]
3. my_tuple[0:]
4. 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 __________.

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.

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])
```
1. (2, 3)
2. (3, 4)
3. (4, 5)
4. (1, 2, 3)