ProgrammingModerateProduct-company MCQ pattern

Which key sorts records by score descending and then by name ascending, using one stable sort?

  1. A. sorted(rows, key=lambda r: (r.score, r.name), reverse=True)
  2. B. sorted(rows, key=lambda r: -r.score - r.total)
  3. C. sorted(rows, key=(r.score, r.name))
  4. D. sorted(rows, key=lambda r: (-r.score, r.name))

Correct answer: D. sorted(rows, key=lambda r: (-r.score, r.name))

Negating the numeric score sorts it descending while the name stays ascending in the same tuple. Reversing the tuple would also reverse the names, and a subtraction with a string or a bare tuple as key is an error.

More Python Programming questions

Exams that ask this topic