w3resource

PCEP Certification Practice: Tuples vs. Lists - Differences and Similarities

PCEP Certification Practice Test - Questions, Answers and Explanations

Here are 25 questions focusing on the topic "tuples vs. lists: similarities and differences" for the PCEP-30-0x 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 is a key difference between tuples and lists in Python?

  1. Tuples are mutable, and lists are immutable.
  2. Tuples are immutable, and lists are mutable.
  3. Both tuples and lists are mutable.
  4. Both tuples and lists are immutable.

Answer: b) Tuples are immutable, and lists are mutable

Explanation: The primary difference is that tuples cannot be modified after creation (immutable), while lists can be modified (mutable).

Question 2: What is the correct syntax to create an empty tuple?

  1. my_tuple = {}
  2. my_tuple = []
  3. my_tuple = ()
  4. my_tuple = [[]]

Answer: c) my_tuple = ()

Explanation: An empty tuple is created using empty parentheses ().

Question 3: Which of the following operations is allowed on both tuples and lists?

  1. Adding elements
  2. Removing elements
  3. Accessing elements by index
  4. Modifying elements

Answer: c) Accessing elements by index

Explanation: Both tuples and lists allow element access using indexing.

Question 4: Which of the following characteristics are true for both tuples and lists? (Choose all that apply)

  1. Both are ordered collections.
  2. Both are immutable.
  3. Both can contain elements of different data types.
  4. Both support slicing.

Answer: a) Both are ordered collections., c) Both can contain elements of different data types., d) Both support slicing.

Explanation: Tuples and lists are ordered, can hold mixed data types, and support slicing. However, only tuples are immutable, so option b) is incorrect.

Question 5: Which of the following operations can be performed on lists but not on tuples? (Choose two)

  1. Adding elements
  2. Accessing elements by index
  3. Removing elements
  4. Reversing the order of elements

Answer: a) Adding elements, c) Removing elements

Explanation: Lists allow adding and removing elements, whereas tuples do not due to their immutability.

Question 6: Tuples are __________, meaning their elements cannot be modified after they are created.

Answer: immutable

Explanation: Tuples are immutable, so their contents cannot be changed once defined.

Question 7: Lists are __________, allowing changes to their elements after they are created.

Answer: mutable

Explanation: Lists are mutable, meaning their elements can be changed, added, or removed.

Question 8: Arrange the following code snippets in the correct order to create a list and a tuple with the same elements, and then print both:

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

Answer:

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

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

Question 9: Arrange the following code snippets in the correct order to access and print the second element of a list and a tuple:

  • my_tuple = ('a', 'b', 'c')
  • my_list = ['x', 'y', 'z']
  • print(my_list[1], my_tuple[1])

Answer:

  • my_list = ['x', 'y', 'z']
  • my_tuple = ('a', 'b', 'c')
  • print(my_list[1], my_tuple[1])

Explanation: The list and tuple are created first, and then the second elements are accessed and printed.

Question 10: Complete the code to create a list and a tuple with the elements "apple", "banana", and "cherry":

my_list = __________
my_tuple = __________

Answer: ['apple', 'banana', 'cherry'], ('apple', 'banana', 'cherry')

Explanation: Lists use square brackets [], and tuples use parentheses ().

Question 11: Complete the code to slice the tuple my_tuple to get the first two elements:

sliced_tuple = my_tuple[__________]

Answer: 0:2

Explanation: The slice my_tuple[0:2] retrieves the first two elements of the tuple.

Question 12: Insert the correct code to modify the first element of a list named my_list and try to modify the first element of a tuple named my_tuple:

my_list[0] = 'new_value'
my_tuple[0] = 'new_value'

Answer:

Explanation: The list element can be modified, but attempting to modify the tuple element will raise a TypeError because tuples are immutable and their elements cannot be modified after creation.

Question 13: Insert the correct code to check if the value 42 is present in both a list and a tuple:

Answer:

if 42 in my_list and 42 in my_tuple:
    print("Found in both")

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))
  1. 2
  2. 3
  3. 4
  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. [1, 20, 3] (1, 2, 3)
  2. Error
  3. [1, 2, 3] (1, 20, 3)
  4. [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.

  1. True
  2. 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.

  1. True
  2. 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?

  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)

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)
  1. ['apple', 'banana', 'cherry']
  2. ['apple', 'blueberry', 'cherry']
  3. Error
  4. d) None

Answer: b) ['apple', 'blueberry', 'cherry']

Explanation: Since lists are mutable, the element at index 1 is successfully updated to 'blueberry'.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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-vs-lists-similarities-and-differences.php