w3resource

len() and sorted() Functions in Python Lists PCEP Exam preparation

PCEP Certification Practice Test - Questions, Answers and Explanations

Here are 25 questions based on the topic “functions: len(), sorted()” in Python collections (Tuples, Dictionaries, Lists, and Strings) designed for the PCEP-30-0x certification. The questions are in different formats like single-select, multiple-select, fill-in-the-gaps, sort, and code insertion.

Question 1: Which function is used to find the number of elements in a list?

  1. sorted()
  2. len()
  3. sum()
  4. range()

Answer: b) len()

Explanation: The len() function returns the number of items in a list or any other collection. The sorted() function is used to sort elements, not count them.

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

fruits = ["apple", "banana", "cherry"]
print(len(fruits))
  1. 1
  2. 2
  3. 3
  4. 4

Answer: c) 3

Explanation: The list fruits contains three items, so the len() function returns 3.

Question 3: Which of the following will sort the list [3, 1, 2] in ascending order?

  1. sorted([3, 1, 2])
  2. len([3, 1, 2])
  3. [3, 1, 2].sort()
  4. reverse([3, 1, 2])

Answer: a) sorted([3, 1, 2])

Explanation: The sorted() function returns a new list with the elements sorted. Option c) would work, but it modifies the list in place and does not return anything.

Question 4: Which of the following are correct uses of the len() function? (Choose two)

  1. len([1, 2, 3])
  2. len(5)
  3. len("hello")
  4. len((1, 2))

Answer: a) len([1, 2, 3]), c) len("hello")

Explanation: The len() function works with collections like lists, strings, and tuples. It does not work with single integers like in option b).

Question 5: Which of the following will return a sorted list in Python? (Choose all that apply)

  1. sorted(["b", "a", "c"])
  2. ["b", "a", "c"].sort()
  3. sorted([5, 3, 1])
  4. sorted({"z": 1, "y": 2, "x": 3})

Answer: a) sorted(["b", "a", "c"]), c) sorted([5, 3, 1]), d) sorted({"z": 1, "y": 2, "x": 3})

Explanation: Option b) modifies the list in place and does not return a new list. Option d) returns a list of the dictionary keys sorted in ascending order.

Question 6: Fill in the blank: The length of the list [10, 20, 30] is __________.

Answer: 3

Explanation: The list contains three elements, so len([10, 20, 30]) returns 3

Question 7: The code sorted(["delta", "alpha", "bravo"]) will return the list __________.

Answer: ['alpha', 'bravo', 'delta']

Explanation: The sorted() function arranges the strings in alphabetical order.

Question 8: Arrange the following code statements in the correct order to sort a list of numbers and print the length of the list:

  1. sorted_numbers = sorted(numbers)
  2. numbers = [7, 2, 5]
  3. print(len(sorted_numbers))

Answer: b) numbers = [7, 2, 5]
a) sorted_numbers = sorted(numbers)
c) print(len(sorted_numbers))

Explanation: The list needs to be defined first, followed by sorting it and finally printing its length.

Question 9: Arrange the following numbers in ascending order as they would appear after being sorted:

8, 3, 10, 2

Answer: 2, 3, 8, 10

Explanation: The numbers are arranged from smallest to largest when using the sorted() function.

Question 10: Complete the code to print the sorted version of the list animals:

animals = ["dog", "cat", "elephant"]
print(__________)

Answer: sorted(animals)

Explanation: The sorted() function should be used to sort and print the list in alphabetical order.

Question 11: Complete the code to print the length of the tuple colors:

colors = ("red", "blue", "green")
print(__________)")

Answer: len(colors)

Explanation: The len() function is used to determine the number of items in the tuple.

Question 12: Insert the correct code to sort the list of numbers [4, 2, 9, 1] and print the result.

numbers = [4, 2, 9, 1]
__________

Answer: print(sorted(numbers))

Explanation: The code should use sorted(numbers) to sort the list and print() to display the sorted result.

Question 13: Insert the correct code to print the length of the string "Python".

word = "Python"
__________

Answer: print(len(word))

Explanation: The len() function returns the number of characters in the string, which is then printed.

Question 14: Rearrange the items to sort the list ['zebra', 'ant', 'giraffe'] in alphabetical order and store it in sorted_animals:

  1. sorted_animals = sorted(animals)
  2. animals = ['zebra', 'ant', 'giraffe']
  3. print(sorted_animals)

Answer:

  1. animals = ['zebra', 'ant', 'giraffe']
  2. sorted_animals = sorted(animals)
  3. print(sorted_animals)

Explanation: First, the list is defined, then it’s sorted and stored, and finally, the result is printed.

Question 15: Organize the steps to determine the number of elements in the dictionary {1: "a", 2: "b", 3: "c"}:

  1. my_dict = {1: "a", 2: "b", 3: "c"}
  2. print(len(my_dict))

Answer: a) my_dict = {1: "a", 2: "b", 3: "c"}
b) print(len(my_dict))

Explanation: The dictionary is created first and then the length is printed using len().

Question 16: What does the following code output?

names = ["John", "Jane", "Doe"]
print(len(sorted(names)))
  1. 3
  2. 2
  3. ["Doe", "Jane", "John"]
  4. None

Answer: a) 3

Explanation: The code sorts the list and then calculates the length, which remains 3 since the number of items doesn’t change.

Question 17: What does the following code output?

mixed_list = ["apple", 3, "banana", 1]
print(sorted(mixed_list))
  1. ['apple', 'banana', 1, 3]
  2. Error
  3. [1, 3, 'apple', 'banana']
  4. None

Answer: b) Error

Explanation: The code results in an error because Python cannot directly compare strings with integers.

Question 18: The len() function can be used to determine the length of both tuples and strings.

  1. True
  2. False

Answer: a) True

Explanation: The len() function works with multiple collection types including strings, tuples, lists, and dictionaries.

Question 19: The sorted() function sorts a list in place without returning a new list.

  1. True
  2. False

Answer: b) False

Explanation: The sorted() function returns a new sorted list, leaving the original list unchanged.

Question 20: What will len([]) return?

  1. 0
  2. 1
  3. None
  4. Error

Answer: a) 0

Explanation: The length of an empty list is 0.

Question 21: What will be the result of sorted("Python")?

  1. ['Python']
  2. ['P', 'h', 'n', 'o', 't', 'y']
  3. ['P', 'y', 't', 'h', 'o', 'n']
  4. "Python"

Answer: b) ['P', 'h', 'n', 'o', 't', 'y']

Explanation: The sorted() function sorts the characters in the string according to their Unicode values, with uppercase letters appearing before lowercase letters.

Question 22: Which of the following operations are valid? (Choose two)

  1. sorted((5, 3, 1))
  2. len({"key1": "value1", "key2": "value2"})
  3. sorted(100)
  4. len(42)

Answer: a) sorted((5, 3, 1)), b) len({"key1": "value1", "key2": "value2"})

Explanation: sorted() works on tuples, and len() can determine the number of keys in a dictionary. The other options involve unsupported types for sorted() and len().

Question 23: The len() function can be used on any __________, while the sorted() function is used to sort collections.

Answer: collection

Explanation: The len() function is applicable to all collection types like lists, tuples, strings, and dictionaries.

Question 24: The function __________ is used to arrange items in a list in a specific order.

Answer: sorted()

Explanation: The sorted() function sorts the items either in ascending or descending order.

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

words = ["pear", "apple", "banana"]
print(len(sorted(words)))
  1. 1
  2. 2
  3. 3
  4. Error

Answer: c) 3

Explanation: The length of the sorted list remains the same as the original list because no items are removed or added.

Test your Python skills with w3resource's quiz



Become a Patron!

Follow us on Facebook and Twitter for latest update.