ProgrammingHardProduct-company MCQ pattern
What is printed on the last line?
What is printed on the last line?
try:
sorted([1, '2'])
except TypeError:
print('TypeError')
print(sorted([1, '2'], key=str))- A. [1, '3']
- B. [1, '2']
- C. ['2', 1]
- D. [1, '2', 2]
Correct answer: B. [1, '2']
Sorting mixed int and str without a key raises TypeError. With key=str both items become text, so '1' sorts before '2' and the original objects are kept in the output.
More Python Programming questions
- 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'))
- What is printed? python a = 1e308 b = a 10 print(b, -b, b - b if False else 0, b > a, b == float('inf'))