Monday, September 14, 2026

Python sorted() Quiz

 Python sorted() Quiz


1. What is the output? print(sorted([3, 1, 2]))

   A. [3, 2, 1]

   B. [1, 2, 3]

   C. None

   D. [2, 1, 3]


2. What is the output? print(sorted([3, 1, 2], reverse=True))

   A. [2, 3, 1]

   B. [3, 2, 1]

   C. None

   D. [1, 2, 3]


3. What is the output? words = ["banana", "fig", "apple"] print(sorted(words, key=len))

   A. ['fig', 'apple', 'banana']

   B. ['banana', 'apple', 'fig']

   C. ['apple', 'banana', 'fig']

   D. ['fig', 'banana', 'apple']


4. What is the output? nums = [10, 2, 30, 4] print(sorted(nums, key=lambda x: x))

   A. [30, 10, 4, 2]

   B. [4, 2, 10, 30]

   C. [10, 2, 4, 30]

   D. [2, 4, 10, 30]


5. What is the output? names = ["Bob", "alice", "Charlie"] print(sorted(names, key=str.lower))

   A. ['Charlie', 'Bob', 'alice']

   B. ['alice', 'Bob', 'Charlie']

   C. ['alice', 'Charlie', 'Bob']

   D. ['Bob', 'Charlie', 'alice']

______________________________


1. What is the output? nums = [7, 2, 9, 1] print(sorted(nums))

   A. [7, 2, 9, 1]

   B. [1, 2, 7, 9]

   C. [2, 1, 7, 9]

   D. [9, 7, 2, 1]


2. What is the output? nums = [4, 8, 1, 6] print(sorted(nums, reverse=True))

   A. [6, 8, 4, 1]

   B. [8, 6, 4, 1]

   C. [4, 8, 1, 6]

   D. [1, 4, 6, 8]


3. What is the output? words = ["pear", "apple", "banana"] print(sorted(words))

   A. ['banana', 'pear', 'apple']

   B. ['pear', 'banana', 'apple']

   C. ['apple', 'pear', 'banana']

   D. ['apple', 'banana', 'pear']


4. What is the output? words = ["cat", "elephant", "dog"] print(sorted(words, key=len))

   A. ['cat', 'elephant', 'dog']

   B. ['dog', 'cat', 'elephant']

   C. ['elephant', 'cat', 'dog']

   D. ['cat', 'dog', 'elephant']


5. What is the output? words = ["one", "three", "two"] print(sorted(words, key=len, reverse=True))

   A. ['one', 'two', 'three']

   B. ['three', 'one', 'two']

   C. ['two', 'one', 'three']

   D. ['three', 'two', 'one']


6. What is the output? items = [("B", 2), ("A", 3), ("C", 1)] print(sorted(items, key=lambda x: x[1]))

   A. [('C', 1), ('B', 2), ('A', 3)]

   B. [('A', 3), ('B', 2), ('C', 1)]

   C. [('A', 3), ('C', 1), ('B', 2)]

   D. [('B', 2), ('A', 3), ('C', 1)]


7. What is the output? students = [("John", 80), ("Amy", 95), ("Bob", 70)] print(sorted(students, key=lambda x: x[1], reverse=True))

   A. [('John', 80), ('Amy', 95), ('Bob', 70)]

   B. [('Amy', 95), ('John', 80), ('Bob', 70)]

   C. [('Bob', 70), ('John', 80), ('Amy', 95)]

   D. [('Amy', 95), ('Bob', 70), ('John', 80)]


8. What is the output? pairs = [(3, 1), (1, 5), (2, 2)] print(sorted(pairs))

   A. [(1, 5), (3, 1), (2, 2)]

   B. [(2, 2), (1, 5), (3, 1)]

   C. [(1, 5), (2, 2), (3, 1)]

   D. [(3, 1), (2, 2), (1, 5)]


9. What is the output? words = ["apple", "kiwi", "banana", "fig"] print(sorted(words, key=lambda x: (len(x), x)))

   A. ['banana', 'apple', 'kiwi', 'fig']

   B. ['fig', 'kiwi', 'apple', 'banana']

   C. ['fig', 'apple', 'kiwi', 'banana']

   D. ['kiwi', 'fig', 'apple', 'banana']

2 comments:

perlknowledge said...

What is the output?

print(sorted(\[3, 1, 2\]))

The values are returned in ascending order by default.

What is the output?

print(sorted(\[3, 1, 2\], reverse=True))

reverse=True makes the sorted result descending.

What is the output?

words = \["banana", "fig", "apple"\] print(sorted(words, key=len))

Sorting by length places the three-letter word first, followed by five and six letters.

What is the output?

nums = \[10, 2, 30, 4\] print(sorted(nums, key=lambda x: x))

The key returns each number itself, so the numbers are sorted numerically in ascending order.

What is the output?

names = \["Bob", "alice", "Charlie"\] print(sorted(names, key=str.lower))

Converting each name to lowercase for comparison produces this alphabetical order.

perlknowledge said...

What is the output?

nums = \[7, 2, 9, 1\] print(sorted(nums))

sorted() places numbers in ascending order by default.

What is the output?

nums = \[4, 8, 1, 6\] print(sorted(nums, reverse=True))

reverse=True sorts the numbers from largest to smallest.

What is the output?

words = \["pear", "apple", "banana"\] print(sorted(words))

Strings are compared lexicographically, giving this alphabetical order.

What is the output?

words = \["cat", "elephant", "dog"\] print(sorted(words, key=len))

The word lengths are 3, 3, and 8, so ascending length produces this order.

What is the output?

words = \["one", "three", "two"\] print(sorted(words, key=len, reverse=True))

The lengths are 5, 3, and 3, so reverse=True places three first and preserves the original order of equal-length words.

What is the output?

items = \[("B", 2), ("A", 3), ("C", 1)\] print(sorted(items, key=lambda x: x\[1\]))

The second tuple element values are 1, 2, and 3, producing this ascending order.

What is the output?

students = \[("John", 80), ("Amy", 95), ("Bob", 70)\] print(sorted(students, key=lambda x: x\[1\], reverse=True))

The key selects scores and reverse=True puts the highest score first.

What is the output?

pairs = \[(3, 1), (1, 5), (2, 2)\] print(sorted(pairs))

Tuples are compared lexicographically, so their first elements determine this order.

What is the output?

words = \["apple", "kiwi", "banana", "fig"\] print(sorted(words, key=lambda x: (len(x), x)))

The tuple key sorts first by length and then alphabetically for equal lengths.