ProgrammingVery hardProduct-company MCQ pattern
What is printed?
What is printed?
print(sorted(['10', '9', '2b', '2a']), sorted([10, 9, 2], key=str))
- A. ['10', '2a', '2b', '9'] [10, 2, 9]
- B. ['10', '2a', '2b', '9'] [2, 9, 10]
- C. ['10', '2a', '2b', '9'] [9, 10, 2]
- D. ['2a', '2b', '10', '9'] [10, 2, 9]
Correct answer: A. ['10', '2a', '2b', '9'] [10, 2, 9]
String sorting compares character by character, so '10' comes before '2a' because '1' is below '2' and '9' comes last. With key=str the integers are compared as text, which puts '10' before '2' and '9'.
More Python Programming questions
- What is printed on the last line? python try: sorted([1, '2']) except TypeError: print('TypeError') print(sorted([1, '2'], key=str))
- What is printed? python print(max([], default=-1), min([3], default=0), sum([]), sum([], 0.0), any([]) or all([]))
- What is printed on the first line? python words = ['apple', 'Bob', 'cat', 'Dog', 'ant'] print(sorted(words, key=lambda w: (w[0].lower()…
- What is printed on the second line? python nums = [5, -3, 0, 2, -3] print(sorted(nums, key=lambda x: x < 0)) print(sorted(nums, key=lambda…
- What is printed on the first line? python data = [('x', 3.0), ('y', 3), ('z', 2.5)] print(sorted(data, key=lambda t: t[1]))…
- What is printed? python s = 'level' t = ''.join(reversed(s)) print(s == t, s[::-1] is s, s[::-1] == t, s.count('l') s.count('e'))