ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
print(sorted([1, 10, 2], key=lambda x: str(x)[0]))
  1. A. [1, 2, 10]
  2. B. [1, 10, 2]
  3. C. [10, 1, 2]
  4. D. [2, 1, 10]

Correct answer: B. [1, 10, 2]

The key is the first character, so 1 and 10 both key to '1' and 2 keys to '2'. The stable sort keeps 1 before 10 because they tie.

More Python Programming questions

Exams that ask this topic