w3resource

Using the len() Function with Python Lists PCEP Exam preparation

PCEP Certification Practice Test - Questions, Answers and Explanations

Here are 25 questions related to the subtopic "the len() function" using lists in Python, formatted according to the PCEP-30-02 1.1 examination style.

Question 1: What will be the output of the following code?

my_list = [10, 20, 30, 40]
print(len(my_list))
  1. 3
  2. 4
  3. 40
  4. None

Answer: b) 4

Explanation: The len() function returns the number of elements in the list. Here, the list my_list has 4 elements, so len(my_list) returns 4.

Question 2: Complete the code to print the length of the list my_list = [1, 2, 3, 4, 5]:

print(___(my_list))

Answer: print(len(my_list))

Explanation: The len() function is used to find the length of the list.

Question 3: What will the following code output?

my_list = []
print(len(my_list))
  1. 0
  2. 1
  3. None
  4. Error

Answer: a) 0

Explanation: An empty list has no elements, so len(my_list) returns 0.

Question 4: Fill in the blank to get the length of the list my_list = ["apple", "banana", "cherry"]:

length = len(___)

Answer: length = len(my_list)

Explanation: You pass the list my_list as an argument to the len() function to get its length.

Question 5: Which of the following correctly calculates the length of the list my_list = [100, 200, 300, 400, 500]?

  1. length = my_list.length()
  2. length = my_list.size()
  3. length = len(my_list)
  4. length = my_list.count()

Answer: c) length = len(my_list)

Explanation: The len() function is the correct way to determine the length of a list in Python.

Question 6: Insert the correct code to check if the list my_list = [1, 2, 3] has more than 2 elements.

Answer:

if len(my_list) > 2:
    print("The list has more than 2 elements.")

Explanation: The len() function is used to determine the number of elements in the list, and a conditional statement checks if it's greater than 2.

Question 7: What will be the output of the following code?

my_list = ["a", "b", "c", "d", "e"]
print(len(my_list[1:4]))
  1. 3
  2. 4
  3. 5
  4. 2

Answer: a) 3

Explanation: The slice my_list[1:4] returns the sublist ["b", "c", "d"], which has 3 elements, so len(my_list[1:4]) returns 3.

Question 8: Complete the code to find the length of a list created using the range() function:

my_list = list(range(10))
length = len(___)

Answer: length = len(my_list)

Explanation: The len() function is used to find the length of the list my_list.

Question 9: What will the following code return?

my_list = [True, False, True, False, True]
print(len(my_list))
  1. 3
  2. 4
  3. 5
  4. 6

Answer: c) 5

Explanation: The list my_list has 5 elements, so len(my_list) returns 5.

Question 10: Which of the following lists have a length of 3? (Select all that apply)

  1. my_list = [1, 2, 3]
  2. my_list = ["a", "b", "c"]
  3. my_list = [True, False]
  4. my_list = [None, None, None]

Answer: a) my_list = [1, 2, 3]
b) my_list = ["a", "b", "c"]
d) my_list = [None, None, None]

Explanation: Lists a, b, and d each have 3 elements, so their length is 3. List c has only 2 elements.

Question 11: Complete the code to check if the list my_list = [5, 10, 15] is non-empty:

if len(my_list) > ___:
    print("The list is non-empty.")

Answer:

if len(my_list) > 0:
    print("The list is non-empty.")

Explanation: A list is non-empty if its length is greater than 0.

Question 12: What will the following code return?

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

Answer: a) 4

Explanation: The list my_list has 4 elements: 1, 2, [3, 4], and 5. The sublist [3, 4] counts as a single element.

Question 13: Insert the correct code to print True if the list my_list = [2, 4, 6, 8] contains exactly 4 elements.

Answer:

if len(my_list) == 4:
    print(True)

Explanation: The len() function is used to check if the length of my_list is equal to 4.

Question 14: Arrange the steps in the correct order to find the length of the list my_list = ["a", "b", "c", "d", "e"]:

  1. print(len(my_list))
  2. my_list = ["a", "b", "c", "d", "e"]

Answer:

  1. my_list = ["a", "b", "c", "d", "e"]
  2. print(len(my_list))

Explanation: First, the list is defined, and then the len() function is used to find and print its length.

Question 15: Which of the following methods correctly calculates the length of a list in Python? (Select all that apply)

  1. len(my_list)
  2. my_list.len()
  3. len(my_list[:])
  4. my_list.__len__()

Answer: a) len(my_list)
c) len(my_list[:])
d) my_list.__len__()

Explanation: The len() function, the __len__() method, and slicing the entire list my_list[:] all correctly return the length of the list. my_list.len() is not a valid method in Python.

Question 16: Complete the code to check if the length of the list my_list = [10, 20, 30] is less than 5:

if len(my_list) < ___:
    print("The list is shorter than 5 elements.")

Answer:

if len(my_list) < 5:
    print("The list is shorter than 5 elements.")

Explanation: The len() function checks if the list has fewer than 5 elements.

Question 17: What will the following code output?

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

Answer: a) 2

Explanation: The list my_list contains 2 elements, each of which is a sublist. Therefore, len(my_list) returns 2.

Question 18: Insert the correct code to find the length of the last element in the list my_list = ["Python", "Java", "C++"].

Answer: length_of_last = len(my_list[-1])

Explanation: The -1 index accesses the last element, and len() calculates its length.

Question 19: Which of the following will return 5?

my_list = ["hello", "world"]
  1. len(my_list)
  2. len(my_list[0])
  3. len(my_list[1])
  4. len(my_list[:])

Answer: b) len(my_list[0])
c) len(my_list[1])

Explanation: my_list[0] and my_list[1] are the string "hello" and "world", which have 5 characters, so len(my_list[0]) and len(my_list[1])returns 5.

Question 20: Which of the following statements are true? (Select all that apply)

  1. len([]) == 0
  2. len([None]) == 1
  3. len(["", ""]) == 2
  4. len([""]) == 0

Answer: a) len([]) == 0
b) len([None]) == 1
c) len(["", ""]) == 2

Explanation: An empty list has length 0. A list with None as an element has length 1. A list with two empty strings has length 2. However, len([""]) is 1 because it contains one empty string.

Question 21: Fill in the blank to check if the list my_list = [1, 2, 3, 4, 5] contains exactly 5 elements:

if len(my_list) == ___:
    print("The list contains exactly 5 elements.")

Answer:

if len(my_list) == 5:
    print("The list contains exactly 5 elements.")

Explanation: The len() function checks if the list's length is equal to 5.

Question 22: Complete the code to calculate the total length of two lists list1 = [1, 2, 3] and list2 = [4, 5]:

total_length = len(list1) + len(___)

Answer: total_length = len(list1) + len(list2)

Explanation: The total length is calculated by adding the lengths of both lists using the len() function.

Question 23: Insert the correct code to print True if the length of the list my_list = ["apple", "banana", "cherry", "date"] is 4.

Answer:

if len(my_list) == 4:
    print(True)

Explanation: The len() function checks if the list has exactly 4 elements.

Question 24: What will the following code output?

my_list = [0, 1, 2, 3]
print(len(my_list) + len([]))
  1. 3
  2. 4
  3. 5
  4. Error

Answer: c) 4

Explanation: len(my_list) is 4 and len([]) is 0, so the sum is 4.

Question 25: Which of the following are correct ways to find the length of the list my_list = [1, "a", True]? (Select all that apply)

  1. len(my_list)
  2. my_list.__len__()
  3. len(my_list[:])
  4. my_list.length()

Answer: a) len(my_list)
b) my_list.__len__()
c) len(my_list[:])

Explanation: The len() function, the __len__() method, and slicing the entire list my_list[:] correctly return the length of the list. my_list.length() is not a valid method in Python.

Test your Python skills with w3resource's quiz



Become a Patron!

Follow us on Facebook and Twitter for latest update.