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, 2], [3, 4])
- ([1, 2, 5], [3, 4])
- ([1, 2, 3], [4, 5])
- ([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)
- t[0].append(3)
- t[0] = [4, 5, 6]
- t[1].remove(2)
- t[1][1] = 10
Answer: A) t[0].append(3)
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)▼
Answer: 25
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
- 3
- 4
- 5
Answer: B) 3
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)▼
Answer: t[0]
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]
- The list inside the tuple will be replaced with [7, 8, 9].
- The tuple will raise a TypeError.
- The tuple will be modified to ([7, 8, 9], [4, 5, 6]).
- 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)
- lst[0].append(4)
- lst[0] = (10, 20, 30)
- lst[1] = lst[1] + (40,)
- 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.
- Print the list with the tuples.
- Define the second tuple with a list.
- Define the first tuple with a list.
- Create the list containing the tuples.
Answer: 3, 2, 4, 1
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: ______▼
Answer: 3
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])
- 3
- 4
- 5
- 6
Answer: D) 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)▼
Answer: 10
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?
- By using the replace() method.
- By reassigning the tuple.
- By directly modifying the list element.
- 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)
- Lists inside tuples can be modified.
- Lists inside tuples cannot be reassigned.
- Tuples inside lists can be reassigned.
- Tuples inside lists can be modified.
Answer: A) Lists inside tuples can be modified.
B) Lists inside tuples cannot be reassigned.
C) Tuples inside lists can be reassigned.
Explanation: Lists inside tuples can be modified but cannot be reassigned. Tuples inside lists can be reassigned, but they cannot be modified.
Question 14: Arrange the steps to create a tuple containing a list and modify an element in the list.
- Modify an element in the list.
- Print the tuple.
- Create a tuple with a list as one of its elements.
- Access the list inside the tuple.
Answer: 3, 4, 1, 2
Explanation: First, create the tuple with a list, then access the list, modify an element in it, and finally print the tuple.
Question 15: Complete the following code to concatenate a new tuple to the existing tuple inside the list lst.
lst = [(1, 2), (3, 4)] lst[1] = lst[1] + (5, 6) print(lst) # Output: [(1, 2), ______]▼
Answer:(3, 4, 5, 6)
Explanation: lst[1] = lst[1] + (5, 6) concatenates (5, 6) to the second tuple, resulting in [(1, 2), (3, 4, 5, 6)].
Question 16: What does the following code output?
t = ([10, 20], [30, 40]) t[0][0] = 100 print(t)
- ([10, 20], [30, 40])
- ([100, 20], [30, 40])
- ([10, 100], [30, 40])
- ([100, 100], [30, 40])
Answer: B) ([100, 20], [30, 40])
Explanation: The code modifies the first element of the first list inside the tuple to 100.
17. Insert the correct code to print the first element of the first list inside the tuple t.
t = ([10, 20], [30, 40]) print(_______.[0])▼
Answer: t[0]
Explanation: t[0][0] accesses and prints the first element of the first list inside the tuple.
Question 18: What will the following code output if you try to modify a tuple inside a list?
lst = [(1, 2), (3, 4)] lst[0][0] = 10
- The code will execute without errors and modify the tuple.
- The code will raise a TypeError.
- The code will modify the list instead.
- The code will raise a ValueError.
Answer: B) The code will raise a TypeError.
Explanation: Tuples are immutable, so trying to modify an element within a tuple will raise a TypeError.
Question 19: Which of the following are true for modifying elements in a list inside a tuple? (Choose all that apply)
- Elements can be added using append().
- Elements can be removed using remove().
- Elements can be reassigned.
- The entire list can be replaced.
Answer: A) Elements can be added using append().
B) Elements can be removed using remove().
C) Elements can be reassigned.
Explanation: You can add, remove, and reassign elements in a list inside a tuple. However, you cannot replace the entire list because the tuple is immutable.
Question 20: Arrange the steps to create a list containing a tuple and then access the second element of the tuple.
- Access the second element of the tuple.
- Create a list containing a tuple.
- Define the tuple with elements.
- Print the accessed element.
Answer: 3, 2, 1, 4
Explanation: First, define the tuple, create a list containing the tuple, access the second element of the tuple, and finally print the accessed element.
Question 21: Complete the following code to access the first element of the second tuple inside the list lst.
lst = [(10, 20), (30, 40)] result = lst[1][0] print(result) # Output: ______▼
Answer: 30
Explanation: lst[1][0] accesses the first element of the second tuple, which is 30.
Question 22: What will be the output of the following code?
lst = [(1, 2), (3, 4)] print(lst[0][1])
- 1
- 2
- 3
- 4
Answer: B) 2
Explanation: lst[0][1] accesses the second element of the first tuple in the list, which is 2.
Question 23: Insert the correct code to replace the tuple inside the list lst with a new tuple.
lst = [(1, 2), (3, 4)] lst[1] = ______ print(lst)▼
Answer: (5, 6)
Explanation: lst[1] = (5, 6) replaces the second tuple in the list with the new tuple (5, 6).
Question 24: How can you remove an element from a list inside a tuple?
- By using the del statement.
- By reassigning the tuple.
- By using the remove() method on the list.
- It is not possible to remove elements from a list inside a tuple.
Answer: C) By using the remove() method on the list.
Explanation: You can use the remove() method to delete elements from a list inside a tuple, as lists are mutable.
Question 25: Which of the following code snippets will successfully modify a list inside a tuple? (Choose all that apply)
t = ([1, 2, 3], [4, 5, 6]) t[0].append(4)
t = ([1, 2, 3], [4, 5, 6]) t[1].remove(5)
t = ([1, 2, 3], [4, 5, 6]) t[0][1] = 10
t = ([1, 2, 3], [4, 5, 6]) t[1] = (7, 8, 9)
Answer: A), B), and C)
Explanation: Options A, B, and C correctly modify the list inside the tuple by appending, removing, or reassigning elements. Option D attempts to reassign the entire list, which raises a TypeError.
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-lists-inside-tuples-and-tuples-inside-lists.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics