w3resource

PCEP Certification Practice Test: Copying and Cloning

PCEP Certification Practice Test - Questions, Answers and Explanations

Here are 25 questions focusing on the topic "copying and cloning lists" 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 methods correctly creates a shallow copy of a list named my_list?

  1. new_list = my_list.copy()
  2. new_list = copy(my_list)
  3. new_list = my_list.clone()
  4. new_list = clone(my_list)

Answer: a) new_list = my_list.copy()

Explanation: The .copy() method is the correct and direct way to create a shallow copy of a list in Python.

Question 2: What is the result of the following code?

original = [1, 2, 3]
copy = original
copy.append(4)
print(original)
  1. [1, 2, 3]
  2. [1, 2, 3, 4]
  3. [4]
  4. []

Answer: b) [1, 2, 3, 4]

Explanation: Assigning copy = original does not create a new list; it creates a reference to the same list. Modifying copy also modifies original.

Question 3: Which of the following is a correct way to create a clone of a list using slicing?

  1. new_list = my_list[:]
  2. new_list = my_list::
  3. new_list = copy(my_list[:])
  4. new_list = clone(my_list)

Answer: a) new_list = my_list[:]

Explanation: The slice operation my_list[:] creates a shallow copy of the list by copying all elements.

Question 4: Which of the following methods create a shallow copy of a list? (Choose all that apply)

  1. list.copy()
  2. list[:]
  3. list.clone()
  4. list.copy.deepcopy()

Answer: a) list.copy(), b) list[:]

Explanation: The .copy() method and slicing both create shallow copies. Options c) and d) are incorrect because .clone() is not a valid list method, and .deepcopy() is a function available in the copy module for deep copying.

Question 5: Which of the following statements are true about copying lists in Python? (Choose two)

  1. Slicing creates a shallow copy of a list.
  2. Using copy.deepcopy() creates a deep copy of a list.
  3. Using list = original_list creates a deep copy.
  4. The .copy() method creates a deep copy.

Answer: a) Slicing creates a shallow copy of a list., b) Using copy.deepcopy() creates a deep copy of a list.

Explanation: Slicing and the .copy() method create shallow copies. The copy.deepcopy() function is used to create deep copies. Assigning list = original_list does not copy the list; it only creates a reference.

Question 6: The expression new_list = __________[:] creates a shallow copy of my_list.

Answer: my_list

Explanation: The slice my_list[:] copies all elements from the original list into a new list.

Question 7: The method __________() is used to create a shallow copy of a list.

Answer: .copy

Explanation: The .copy() method is specifically designed to create shallow copies of lists in Python.

Question 8: Arrange the following code statements in the correct order to create a deep copy of a list:

  1. import copy
  2. deep_copied_list = copy.deepcopy(original_list)
  3. original_list = [[1, 2], [3, 4]]

Answer: a) import copy
c) original_list = [[1, 2], [3, 4]]
b) deep_copied_list = copy.deepcopy(original_list)

Explanation: First, the copy module is imported, the original list is defined, and then a deep copy is created using copy.deepcopy().

Question 9: Arrange the following code snippets in the correct order to clone a list using slicing:

  1. print(cloned_list)
  2. cloned_list = my_list[:]
  3. my_list = [10, 20, 30]

Answer: c) my_list = [10, 20, 30]

b) cloned_list = my_list[:]
a) print(cloned_list)

Explanation: The list is defined first, a shallow copy is created using slicing, and then the cloned list is printed.

Question 10: Complete the code to create a shallow copy of the list numbers:

numbers = [1, 2, 3, 4, 5]
copy_of_numbers = __________

Answer: numbers.copy()

Explanation: The .copy() method creates a shallow copy of the list.

Question 11: Complete the code to create a deep copy of a nested list named matrix:

import __________
matrix_copy = copy.deepcopy(matrix)

Answer: copy

Explanation: The copy module provides the deepcopy() function, which is necessary for deep copying nested lists.

Question 12: 12. Insert the correct code to create a shallow copy of a list using slicing:

original = ["apple", "banana", "cherry"]
clone = __________

Answer: original[:]

Explanation: The slice original[:] creates a shallow copy of the list.

Question 13: Insert the correct code to create a deep copy of a list named nested_list:

import copy
deep_clone = __________

Answer: copy.deepcopy(nested_list)

Explanation: The function copy.deepcopy() is used to create deep copies of lists, including nested lists.

Question 14: Rearrange the code to create a shallow copy of a list using the .copy() method:

  1. print(copied_list)
  2. copied_list = original.copy()
  3. original = [100, 200, 300]

Answer: c) original = [100, 200, 300]
b) copied_list = original.copy()
a) print(copied_list)

Explanation: The list is defined first, a shallow copy is created using .copy(), and then the copied list is printed.

Question 15: Organize the steps to create a deep copy of a list containing nested elements:

  1. import copy
  2. deep_copied_list = copy.deepcopy(original_list)
  3. original_list = [[1, 2], [3, 4]]

Answer: a) import copy
c) original_list = [[1, 2], [3, 4]]
b) deep_copied_list = copy.deepcopy(original_list)

Explanation: First, the copy module is imported, the original list is defined, and then a deep copy is created using copy.deepcopy().

Question 16: What does the following code output?

original = [1, 2, 3]
clone = original[:]
clone.append(4)
print(original)
  1. [1, 2, 3]
  2. [1, 2, 3, 4]
  3. [4]
  4. []

Answer: a) [1, 2, 3]

Explanation: The slice original[:] creates a shallow copy of the list. Modifying clone does not affect original.

Question 17: What does the following code output?

original = [[1, 2], [3, 4]]
shallow_copy = original.copy()
shallow_copy[0].append(5)
print(original)
  1. [[1, 2], [3, 4]]
  2. [[1, 2, 5], [3, 4]]
  3. [[1], [3, 4]]
  4. [[1, 5], [3, 4]]

Answer: b) [[1, 2, 5], [3, 4]]

Explanation: The .copy() method creates a shallow copy, which means the inner lists are still shared. Modifying one of the inner lists in the shallow copy affects the original list.

Question 18: Using slicing to clone a list creates a deep copy of the list.

  1. True
  2. False

Answer: b) False

Explanation: Slicing creates a shallow copy, not a deep copy. Changes to mutable objects within the shallow copy affect the original list.

Question 19: The copy.deepcopy() function creates a copy of a list and all its nested elements.

  1. True
  2. False

Answer: a) True

Explanation: The copy.deepcopy() function creates a completely independent copy of a list, including any nested lists or objects.

Question 20: What happens if you attempt to clone a list with mutable elements using slicing?

  1. The cloned list shares the same elements with the original list.
  2. The cloned list is independent of the original list.
  3. Changes to nested elements in the clone affect the original list.
  4. The original list is emptied.

Answer: a) The cloned list shares the same elements with the original list., c) Changes to nested elements in the clone affect the original list.

Explanation: Slicing creates a shallow copy. If the original list contains mutable elements (like other lists), changes to these elements will affect both the original and cloned lists.

Question 21: Which of the following statements about deep copying lists is correct?

  1. Deep copying is slower than shallow copying.
  2. Deep copying is unnecessary for lists with only primitive data types.
  3. Deep copying creates an independent copy of both the outer list and all nested elements.
  4. Deep copying is done using the .copy() method.

Answer: a) Deep copying is slower than shallow copying., c) Deep copying creates an independent copy of both the outer list and all nested elements.

Explanation: Deep copying is slower because it recursively copies all elements, including nested ones. Deep copying ensures that both the outer list and all nested lists (or other mutable objects) are copied, making the new copy completely independent of the original.

Question 22: Which of the following methods should you use if you need to clone a list containing nested lists?

  1. list.copy()
  2. list[:]
  3. copy.deepcopy(list)
  4. list.clone()

Answer: c) copy.deepcopy(list)

Explanation: The copy.deepcopy() function ensures that all nested elements are fully cloned, creating an independent copy.

Question 23: Shallow copies do not clone nested __________ within a list.

Answer: objects

Explanation: Shallow copies do not clone nested objects, meaning they share references with the original list.

Question 24: The deepcopy() function is part of the __________ module.

Answer: copy

Explanation: The deepcopy() function is provided by the copy module.

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

original = [1, 2, 3]
new_copy = original.copy()
new_copy.append(4)
print(original)
  1. [1, 2, 3]
  2. [1, 2, 3, 4]
  3. [4]
  4. None

Answer: a) [1, 2, 3]

Explanation: The .copy() method creates a shallow copy. Changes to new_copy do not affect original.

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.