ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
print(sorted(['1.10', '1.9', '1.10.1']))
  1. A. ['1.10', '1.10.1', '1.9']
  2. B. ['1.9', '1.10', '1.10.1']
  3. C. ['1.10.1', '1.10', '1.9']
  4. D. ['1.9', '1.10.1', '1.10']

Correct answer: A. ['1.10', '1.10.1', '1.9']

Plain sorting compares strings character by character, so '1.10' and '1.10.1' share a prefix and the shorter one comes first. '1.9' is last because '9' is greater than '1'.

More Python Programming questions

Exams that ask this topic