ProgrammingVery hardTCS NQT · Accenture

What is printed?

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

Correct answer: C. 3 ['a', 'b', 'c']

Sorting by negated value puts 3 first. The key c > 'a' is False only for a, so a sorts first, and the stable order keeps b before c.

More Python Programming questions

Exams that ask this topic