# PCEP Certification Practice: Lists inside Tuples and Tuples inside Lists

## PCEP Certification Practice Test - Questions, Answers and Explanations

Below is a set of 25 questions for the Certified Entry-Level Python Programmer (PCEP) examination focusing on the subtopic "lists inside tuples and tuples inside lists." The questions use various formats including single- and multiple-select questions, fill-in-the-gap, code fill, code insertion, sorting, and more.

Question 1: What will be the output of the following code?

```t = ([1, 2], [3, 4])
t[0].append(5)
print(t)
```
1. ([1, 2], [3, 4])
2. ([1, 2, 5], [3, 4])
3. ([1, 2, 3], [4, 5])
4. ([5, 1, 2], [3, 4])

Answer: B) ([1, 2, 5], [3, 4])

Explanation: The list inside the tuple is mutable, so append(5) adds 5 to the first list inside the tuple.

Question 2: Which of the following operations are valid for a list inside a tuple? (Choose all that apply)

1. t[0].append(3)
2. t[0] = [4, 5, 6]
3. t[1].remove(2)
4. t[1][1] = 10

C) t[1].remove(2)
D) t[1][1] = 10

Explanation: Lists inside tuples can be modified (e.g., appending or removing elements), but the list object itself cannot be reassigned.

Question 3: Complete the following code to change the value of the second element of the list inside the tuple t.

```t = ([10, 20, 30], [40, 50])
t[0][1] = ______
print(t)
```

Explanation: t[0][1] = 25 changes the second element of the first list inside the tuple to 25.

Question 4: What will be the output of the following code?

```lst = [(1, 2), (3, 4), (5, 6)]
print(lst[1][0])
```
1. 1
2. 3
3. 4
4. 5

Explanation: lst[1][0] accesses the first element of the second tuple in the list, which is 3.

Question 5: Insert the correct code to add 7 to the list inside the tuple t.

```t = ([1, 2, 3],)
_______.append(7)
print(t)
```

Explanation: t[0].append(7) adds 7 to the list inside the tuple t.

Question 6: What will happen if you try to reassign an entire list inside a tuple?

```t = ([1, 2, 3], [4, 5, 6])
t[0] = [7, 8, 9]
```
1. The list inside the tuple will be replaced with [7, 8, 9].
2. The tuple will raise a TypeError.
3. The tuple will be modified to ([7, 8, 9], [4, 5, 6]).
4. The code will run without errors but nothing will change.

Answer: B) The tuple will raise a TypeError.

Explanation: You cannot reassign an entire list inside a tuple because tuples are immutable. Trying to do so raises a TypeError.

Question 7: Which of the following operations can you perform on a tuple inside a list? (Choose all that apply)

1. lst[0].append(4)
2. lst[0] = (10, 20, 30)
3. lst[1] = lst[1] + (40,)
4. lst[0].remove(2)

Answer: B) lst[0] = (10, 20, 30)
C) lst[1] = lst[1] + (40,)

Explanation: You can reassign a tuple inside a list or concatenate it with another tuple, but tuples do not support append() or remove() methods.

Question 8: Arrange the steps to create a list containing two tuples, where each tuple contains a list of numbers.

1. Print the list with the tuples.
2. Define the second tuple with a list.
3. Define the first tuple with a list.
4. Create the list containing the tuples.

Explanation: First, define the tuples, then create the list containing the tuples, and finally, print the list.

Question 9: Complete the following code to access the third element in the first tuple inside the list lst.

```lst = [(1, 2, 3), (4, 5, 6)]
result = lst[0][2]
print(result)  # Output: ______
```

Explanation: lst[0][2] accesses the third element of the first tuple, which is 3.

Question 10: What will be the output of the following code?

```t = ([1, 2, 3], [4, 5, 6])
print(t[1][2])
```
1. 3
2. 4
3. 5
4. 6

Explanation: t[1][2] accesses the third element of the second list inside the tuple, which is 6.

Question 11: Insert the correct code to change the second element of the second tuple inside the list lst.

```lst = [(1, 2, 3), (4, 5, 6)]
lst[1] = (4, ______, 6)
print(lst)
```

Explanation: lst[1] = (4, 10, 6) changes the second element of the second tuple to 10.

Question 12: How can you modify an element in a list inside a tuple?

1. By using the replace() method.
2. By reassigning the tuple.
3. By directly modifying the list element.
4. It is not possible to modify elements in a list inside a tuple.

Answer: C) By directly modifying the list element.

Explanation: Although tuples are immutable, the lists inside them are mutable, allowing you to modify their elements directly.

Question 13: Which of the following statements are true regarding lists inside tuples? (Choose all that apply)

1. Lists inside tuples can be modified.
2. Lists inside tuples cannot be reassigned.
3. Tuples inside lists can be reassigned.
4. Tuples inside lists can be modified.

Explanation: The in operator works for both lists and tuples, checking for membership.

Question 14: Rearrange the code to create a tuple, access its first element, and print the result:

• first_element = numbers[0]
• print(first_element)
• numbers = (10, 20, 30)

• numbers = (10, 20, 30)
• first_element = numbers[0]
• print(first_element)

Explanation: The tuple is created first, the first element is accessed, and then it is printed.

Question 15: Organize the steps to create a list and a tuple, add an element to the list, and print both:

• my_list = [1, 2, 3]
• my_list.append(4)
• my_tuple = (1, 2, 3)
• print(my_list, my_tuple)

• my_list = [1, 2, 3]
• my_tuple = (1, 2, 3)
• my_list.append(4)
• print(my_list, my_tuple)

Explanation: The list and tuple are created first, the list is modified, and then both are printed.

Question 16: What does the following code output?

```my_tuple = (5, 10, 15)
print(len(my_tuple))
```
1. 2
2. 3
3. 4
4. 5

Explanation: The len() function returns the number of elements in the tuple, which is 3.

17. What does the following code output?

```my_list = [1, 2, 3]
my_tuple = (1, 2, 3)
my_list[1] = 20
my_tuple[1] = 20
print(my_list, my_tuple)
```
1. [1, 20, 3] (1, 2, 3)
2. Error
3. [1, 2, 3] (1, 20, 3)
4. [1, 20, 3] (1, 20, 3)

Explanation: Attempting to modify a tuple’s element raises a TypeError because tuples are immutable.

Question 18: Lists are immutable, while tuples are mutable.

1. True
2. False

Explanation: The statement is false; tuples are immutable, while lists are mutable.

Question 19: Both lists and tuples allow duplicate elements.

1. True
2. False

Explanation: Both lists and tuples can contain duplicate elements.

Question 20: What happens if you try to change the value of an element in a tuple?

1. The element is updated.
2. A new tuple is created with the updated value.
3. An IndexError is raised.
4. A TypeError is raised.

Answer: d) A TypeError is raised.

Explanation: Tuples are immutable, so attempting to change an element raises a TypeError.

Question 21: Which of the following operations can be performed on tuples but not on lists?

1. Accessing elements by index
2. Concatenation
3. Iteration
4. None of the above

Answer: d) None of the above

Explanation: All listed operations (indexing, concatenation, and iteration) can be performed on both tuples and lists.

Question 22: Which of the following expressions correctly concatenates two tuples?

1. tuple1 + tuple2
2. tuple1.append(tuple2)
3. tuple1.extend(tuple2)
4. tuple1.union(tuple2)

Explanation: Tuples can be concatenated using the + operator.

Question 23: Tuples are often used to store data that should not be __________.

Explanation: Tuples are used when the data is intended to remain constant and unchanged.

Question 24: Lists are __________ collections, while tuples are immutable collections.

Explanation: Lists are mutable, allowing modifications, while tuples are immutable.

Question 25: What is the output of the following code?

```fruits = ['apple', 'banana', 'cherry']
fruits[1] = 'blueberry'
print(fruits)
```
1. ['apple', 'banana', 'cherry']
2. ['apple', 'blueberry', 'cherry']
3. Error
4. d) None