ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
print(sorted([1, 10, 2], key=lambda x: str(x)[0]))
- A. [1, 2, 10]
- B. [1, 10, 2]
- C. [10, 1, 2]
- D. [2, 1, 10]
Correct answer: B. [1, 10, 2]
The key is the first character, so 1 and 10 both key to '1' and 2 keys to '2'. The stable sort keeps 1 before 10 because they tie.
More Python Programming questions
- What is printed? python print(0.1 + 0.1 == 0.2, 0.1 + 0.1 + 0.1 == 0.3, 0.2 + 0.2 == 0.4)
- What is printed? python print(list(range(2, 20, 5)), list(range(-1, -10, -4)))
- What is printed? python print(len(str(71000)), 71000 % 10)
- What is printed? python print(10 -3 1000 == 1, 10 -3 1000)
- What is printed? python print(int('12' 2) + int('3' 2), len({'x' n for n in range(4)}))
- What is printed? python print(bin(6 & -6), bin(6 | -6), bin(6 ^ -6))