w3resource

PCEP Certification Practice Test: Mastering the Python del Instruction

PCEP Certification Practice Test - Questions, Answers and Explanations

Here are 24 questions focusing on the Python del instruction within the context of lists for the PCEP-30-02 certification exam. The questions are presented in various formats, including single-select, multiple-select, gap fill, code insertion, sort, and "rearrange" questions. Each question includes the correct answer and an explanation.

Question 1: What is the purpose of the del instruction in Python?

  1. To remove an item from a list by its value
  2. To delete an item from a list by its index
  3. To sort a list in place
  4. To clear all elements from a list

Answer: b) To delete an item from a list by its index

Explanation: The del instruction is used to remove an element from a list by its index or to delete entire variables.

Question 2: What happens if you use del to delete an element with an index that is out of range in a list?

  1. The element at the last index is deleted
  2. The list is cleared
  3. An IndexError is raised
  4. The list remains unchanged

Answer: c) An IndexError is raised

Explanation: Attempting to delete an element at an invalid index using del results in an IndexError.

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

numbers = [10, 20, 30, 40]
del numbers[1]
print(numbers)
  1. [10, 20, 30, 40]
  2. [10, 30, 40]
  3. [20, 30, 40]
  4. [10, 40]

Answer: b) [10, 30, 40]

Explanation: The del instruction removes the element at index 1 (20), resulting in the list [10, 30, 40].

Question 4: Which of the following are valid uses of the del instruction? (Choose all that apply)

  1. del my_list[2]
  2. del my_list[2:4]
  3. del my_list
  4. del my_list[5:]

Answer: a) del my_list[2], b) del my_list[2:4], c) del my_list, d) del my_list[5:]

Explanation: The del instruction can be used to delete a single element by index, a slice of elements, or the entire list.

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

numbers = [1, 2, 3, 4, 5]
del numbers[:]
print(numbers)
  1. []
  2. [1, 2, 3, 4, 5]
  3. None
  4. Error

Answer: a) []

Explanation: The del numbers[:] statement clears the entire list, leaving an empty list.

Question 6: The del instruction can be used to delete a list slice using the format ________.

Answer: del list[start:end]

Explanation: The del instruction is used to delete a slice of elements from index start to end-1.

Question 7: After the code del my_list[2], the item at index __________ is removed from the list.

Answer: 2

Explanation: The del statement deletes the item at the specified index.

Question 8: Arrange the following code statements in the correct order to delete the third element of a list and then print the updated list:

  1. del my_list[2]
  2. my_list = [1, 2, 3, 4, 5]
  3. print(my_list)

Answer:

  1. my_list = [1, 2, 3, 4, 5]
  2. del my_list[2]
  3. print(my_list)

Explanation: The list is defined first, then the third element (index 2) is deleted, and finally, the updated list is printed.

Question 9: Arrange the following code snippets in the order that will delete a slice of a list from index 1 to 3 (inclusive):

  1. del my_list[1:4]
  2. my_list = [10, 20, 30, 40, 50]
  3. print(my_list)

Answer: b) my_list = [10, 20, 30, 40, 50]
a) del my_list[1:4]
c) print(my_list)

Explanation: The list is defined first, then the slice from index 1 to 3 is deleted, and finally, the list is printed.

Question 10: Complete the code to delete the last element from the list colors:

colors = ["red", "green", "blue", "yellow"]
del __________

Answer: colors[-1]

Explanation: Using del colors[-1] deletes the last item in the list.

Question 11: Complete the code to delete a range of elements from index 1 to 3 from the list items:

items = [1, 2, 3, 4, 5, 6]
del __________

Answer: items[1:4]

Explanation: del items[1:4] deletes elements from index 1 to 3.

Question 12: Insert the correct code to delete the element "banana" from the list fruits:

fruits = ["apple", "banana", "cherry"]
__________

Answer: del fruits[1]

Explanation: "banana" is at index 1, so del fruits[1] removes it from the list.

Question 13: Insert the correct code to delete the first three elements from the list data:

data = [10, 20, 30, 40, 50]
__________

Answer: del data[:3]

Explanation: del data[:3] deletes elements at index 0, 1, and 2.

Question 14: Rearrange the code to delete the second and third elements from a list:

  1. del numbers[1:3]
  2. numbers = [5, 10, 15, 20]
  3. print(numbers)

Answer: b) numbers = [5, 10, 15, 20]
a) del numbers[1:3]
c) print(numbers)

Explanation: The list is defined first, then the specified elements are deleted, and the result is printed.

Question 15: Organize the steps to remove a slice from a list:

  1. my_list = [1, 2, 3, 4, 5, 6]
  2. print(my_list)
  3. del my_list[2:5]

Answer: a) my_list = [1, 2, 3, 4, 5, 6]
c) del my_list[2:5]
b) print(my_list)

Explanation: The list is first defined, then the slice is removed, and finally, the updated list is printed.

Question 16: What does the following code output?

elements = [10, 20, 30, 40, 50]
del elements[2:4]
print(elements)
  1. [10, 20, 50]
  2. [10, 30, 50]
  3. [10, 20, 40, 50]
  4. [10, 20, 30]

Answer: a) [10, 20, 50]

Explanation: The slice elements[2:4] (30, 40) is deleted, resulting in [10, 20, 50].

Question 17: What does the following code output?

items = [7, 8, 9, 10, 11]
del items[:3]
print(items)
  1. [7, 8, 9]
  2. [10, 11]
  3. [8, 9, 10, 11]
  4. [7, 8, 9, 10]

Answer: b) [10, 11]

Explanation: The slice items[:3] (7, 8, 9) is deleted, leaving [10, 11].

Question 18: The del instruction can be used to delete an entire list in Python.

  1. True
  2. False

Answer: a) True

Explanation: The del instruction can delete an entire list, removing it from memory.

Question 19: Using del to delete a slice of a list modifies the original list.

  1. True
  2. False

Answer: a) True

Explanation: The del instruction directly modifies the original list by removing specified elements.

Question 20: What will del my_list[-1] do?

  1. Raise an IndexError
  2. Delete the first element
  3. Delete the last element
  4. Do nothing

Answer: c) Delete the last element

Explanation: Using del my_list[-1] deletes the last element of the list, as negative indices count from the end.

Question 21: What happens when you use del on an empty list?

  1. The list remains empty
  2. An IndexError is raised
  3. The list is deleted
  4. Nothing changes

Answer: c) The list is deleted

Explanation: Deleting from an empty list does nothing because there are no elements to remove. This is because the del statement removes the entire list object, not just its elements.

Question 22: Which of the following operations will clear a list completely? (Choose two)

  1. del my_list[:]
  2. my_list.clear()
  3. my_list.remove(my_list[0])
  4. my_list = []

Answer: a) del my_list[:], b) my_list.clear()

Explanation: del my_list[:] and my_list.clear() both empty the list entirely. Option d) creates a new empty list, leaving the original one unchanged.

Question 23: The del instruction deletes elements based on their __________ in a list.

Answer: index

Explanation: The del statement targets elements by their index or a slice of indices in a list.

Question 24: To delete a slice of a list, the correct syntax is del list[start:__________].

Answer: end

Explanation: The slice notation in Python is start:end, where end is non-inclusive.

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.