ProgrammingModerateTCS NQT · Infosys

What is printed?

What is printed?
print(sorted(['b', 'a', 'c'], key=lambda s: -ord(s)))
  1. A. ['c', 'b', 'a']
  2. B. ['a', 'b', 'c']
  3. C. ['b', 'a', 'c']
  4. D. ['c', 'a', 'b']

Correct answer: A. ['c', 'b', 'a']

The key negates the code point, so larger letters have smaller keys and come first. This sorts the letters in reverse alphabetical order.

More Python Programming questions

Exams that ask this topic