ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
print(sorted([10, 2, 33], key=str), sorted(['10', '2', '33'], key=int))
- A. [10, 2, 33] ['10', '2', '33']
- B. [10, 2, 33] ['2', '10', '33']
- C. [10, 33, 2] ['2', '10', '33']
- D. [2, 10, 33] ['2', '10', '33']
Correct answer: B. [10, 2, 33] ['2', '10', '33']
key=str compares the numbers as text, so '10' < '2' < '33' and the original ints keep their order by this key. key=int converts the strings to numbers, so the second list is numerically sorted.
More Python Programming questions
- What is printed? python print('%.2f' % 0.125, '%.2f' % 0.375, '%.1f' % 0.05, '%.0f' % 1.5)
- What is printed? python print(-17 % -5, 17 % -5, -17 % 5, -(17 % 5))
- What is printed? python a = 'ab' b = a 3 print(b.count('ab'), b.count('aba'), len(b.split('b')))
- What is printed? python print(0.1 10 == 1.0, 0.1 10, sum([0.1] 10) == 1)
- What is printed? python print(int(1e15 + 0.3) == 1015, round(1e15 + 0.3) - 1015)
- What is printed? python print(10.01, 0x1F, 1000.0e-3)