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:
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)
Answer:
- 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)
Answer:
- 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))
- 2
- 3
- 4
- 5
Answer: b) 3
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, 20, 3] (1, 2, 3)
- Error
- [1, 2, 3] (1, 20, 3)
- [1, 20, 3] (1, 20, 3)
Answer: b) Error
Explanation: Attempting to modify a tuple’s element raises a TypeError because tuples are immutable.
Question 18: Lists are immutable, while tuples are mutable.
- True
- False
Answer: b) False
Explanation: The statement is false; tuples are immutable, while lists are mutable.
Question 19: Both lists and tuples allow duplicate elements.
- True
- False
Answer: a) True
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?
- The element is updated.
- A new tuple is created with the updated value.
- An IndexError is raised.
- 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?
- Accessing elements by index
- Concatenation
- Iteration
- 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?
- tuple1 + tuple2
- tuple1.append(tuple2)
- tuple1.extend(tuple2)
- tuple1.union(tuple2)
Answer: a) tuple1 + tuple2
Explanation: Tuples can be concatenated using the + operator.
Question 23: Tuples are often used to store data that should not be __________.
▼Answer: changed
Explanation: Tuples are used when the data is intended to remain constant and unchanged.
Question 24: Lists are __________ collections, while tuples are immutable collections.
▼Answer: mutable
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)
- ['apple', 'banana', 'cherry']
- ['apple', 'blueberry', 'cherry']
- Error
- d) None
Answer: b) ['apple', 'blueberry', 'cherry']
Explanation: Since lists are mutable, the element at index 1 is successfully updated to 'blueberry'.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics