ProgrammingVery hardProduct-company MCQ pattern

What is printed on the second line?

What is printed on the second line?
items = [3, -1, 2, -3, 1]
print(sorted(items, key=abs))
print(sorted(items, key=abs, reverse=True))
print(sorted(items, key=lambda x: (abs(x), x)))
  1. A. [3, 2, -3, -1, 1]
  2. B. [3, -3, 2, -1, 1]
  3. C. [3, -3, 1, -1, 2]
  4. D. [-1, 1, 2, 3, -3]

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

With reverse=True, sorted keeps equal-key items in their original order, so 3 comes before -3 and -1 before 1. The trap is assuming reverse flips the whole stable order.

More Python Programming questions

Exams that ask this topic