ProgrammingHardProduct-company MCQ pattern
What is printed on the second line?
What is printed on the second line?
print(''.join(sorted('banana')))
print(sorted('Banana', key=str.lower))
print(sorted(['b10', 'b9', 'a']))- A. ['B', 'a', 'a', 'a', 'n', 'n']
- B. ['a', 'a', 'a', 'n', 'n', 'B']
- C. ['a', 'a', 'a', 'B', 'n', 'n']
- D. ['a', 'a', 'a', 'B', 'n']
Correct answer: C. ['a', 'a', 'a', 'B', 'n', 'n']
Sorting with key=str.lower compares the lowercase forms, so the three a's come first, then B, then the two n's. Without a key, uppercase letters would sort before all lowercase letters, which is the trap in the second option.
More Python Programming questions
- What is printed? python print(min([3, 1.0, True]), max([1, True, 1.0]), max([True, 1.0]), min([1.0, True]))
- What is printed? python print(list(range(10, 0, -3)), range(0, 10, 3)[-1], len(range(5, 1)), range(3) == range(0, 3, 1), range(0, 6, 2) ==…
- What is printed? python print(sum(range(106)) == 106 (106 - 1) // 2, 20.5 20.5 == 2.0, 20.5 20.5)
- What is printed? python print(int(2.9999999999999996), float('1e400'), -float('1e400'), 1e308 10 == float('inf'))
- What is printed? python print(10 % 3.5, -10 % 3.5, 10 // 3.5, round(-1.5), round(0.5) + round(1.5))
- What is printed? python print(divmod(7.5, 2), 7.5 % -2, -7.5 // 2)