ProgrammingVery hardTCS NQT · Accenture

What is printed?

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

Correct answer: A. ['9', '8', '10', '100'] ['8', '9', '10', '100']

Sorting by len is stable, so the one-character strings keep their input order: 9 then 8. Sorting by int orders the strings numerically, giving 8, 9, 10, 100.

More Python Programming questions

Exams that ask this topic