ProgrammingVery hardProduct-company MCQ pattern

What is printed?

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

Correct answer: B. [0, False, 0.5, -0.5, True]

The keys are 0.5, 0.5, 0, 1 and 0, and the stable sort keeps the input order for ties. So 0 comes before False, 0.5 before -0.5, and True last.

More Python Programming questions

Exams that ask this topic