ProgrammingVery hardTCS NQT · Accenture

What is printed?

What is printed?
print(sorted([3, -3, 2, -2, 1], key=abs))
  1. A. [-3, -2, 1, 2, 3]
  2. B. [3, -3, 2, -2, 1]
  3. C. [1, 2, -2, 3, -3]
  4. D. [1, 2, -2, 3, -4]

Correct answer: C. [1, 2, -2, 3, -3]

The key abs ties 3 with -3 and 2 with -2. Python's sort is stable, so each tied pair stays in input order, with 3 before -3 and 2 before -2.

More Python Programming questions

Exams that ask this topic