ProgrammingHardTCS NQT · Infosys

What is printed on the last line?

What is printed on the last line?
nums = ['10', '9', '100', '1']
print(sorted(nums))
print(sorted(nums, key=int))
print(max(nums), max(nums, key=int))
  1. A. 9 100
  2. B. 100 100
  3. C. 9 9
  4. D. 100 9

Correct answer: A. 9 100

Strings compare character by character, so '9' is the largest string. With key=int the comparison uses numeric values, so '100' wins.

More Python Programming questions

Exams that ask this topic