ProgrammingHardTCS NQT · Infosys
What is printed?
What is printed?
print(sorted(['10', '9', '100', '2']), sorted(map(int, ['10', '9', '100', '2'])))
- A. ['2', '9', '10', '100'] [2, 9, 10, 100]
- B. ['10', '100', '2', '9'] [2, 9, 10, 100]
- C. ['10', '100', '2', '9'] [10, 9, 100, 2]
- D. ['10', '9', '100', '2'] [2, 9, 10, 100]
Correct answer: B. ['10', '100', '2', '9'] [2, 9, 10, 100]
Sorting strings compares them character by character, so '10' < '100' < '2' < '9'. Converting to int first gives numeric order, which is the usual fix for this trap.
More Python Programming questions
- What is printed? python print(-7 // -2, 7 // -2 -2 + 7 % -2)
- What is printed? python print(round(-2.5), round(-3.5), round(7, -1), round(15, -1), round(25, -1))
- What is printed? python print(int('12abc', 36), int('zz', 36), int('-z', 36))
- What is printed? python print(hash(-1) == hash(-2), hash(-1), hash(261 - 1))
- What is printed? python print(int.bitlength(-255), (255).bitlength(), (256).bitlength())
- What is printed? python print(chr(0x1F600) == '😀', len('😀'), len('😀'.encode('utf-16-le')))