ProgrammingModerateTCS NQT · Infosys

What is printed?

What is printed?
print(max(['10', '9']), max(['10', '9'], key=len), sorted(['10', '9', '100'], key=lambda s: (len(s), s)))
  1. A. 10 10 ['9', '10', '100']
  2. B. 9 10 ['9', '10', '100']
  3. C. 9 9 ['9', '10', '100']
  4. D. 9 10 ['10', '9', '100']

Correct answer: B. 9 10 ['9', '10', '100']

Strings compare character by character, so '9' is greater than '10'. With key=len the longer '10' wins, and the (length, text) key sorts the one-digit string first.

More Python Programming questions

Exams that ask this topic