ProgrammingModerateTCS NQT · Accenture

What is printed?

What is printed?
nums = [10, 9, 100, 1]
print(sorted(nums, key=str), max(nums, key=str), sorted(map(str, nums))[-1])
  1. A. [1, 9, 10, 100] 100 9
  2. B. [1, 10, 100, 9] 9 9
  3. C. [1, 10, 100, 9] 100 9
  4. D. [1, 10, 100, 9] 9 100

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

Sorting with key=str compares decimal strings, so '1' < '10' < '100' < '9'. The largest string is '9', which gives 9 for max and for the last sorted string.

More Python Programming questions

Exams that ask this topic