ProgrammingHardTCS NQT · Infosys
What is printed?
What is printed?
print(sorted(['apple', 'Banana', 'cherry'], key=str.lower), sorted(['apple', 'Banana', 'cherry']))
- A. ['Banana', 'apple', 'cherry'] ['apple', 'Banana', 'cherry']
- B. ['apple', 'Banana', 'cherry'] ['Banana', 'apple', 'cherry']
- C. ['apple', 'Banana', 'cherry'] ['apple', 'Banana', 'cherry']
- D. ['Banana', 'apple', 'cherry'] ['Banana', 'apple', 'cherry']
Correct answer: B. ['apple', 'Banana', 'cherry'] ['Banana', 'apple', 'cherry']
With key=str.lower, banana sorts after apple but before cherry. Without a key, the uppercase B (code point 66) sorts before every lowercase letter, so Banana comes first.
More Python Programming questions
- What is printed? python print(sum(range(5)) / len(range(5)), sum([1, 2, 3, 4]) // 4, 9 // 2 2 + 9 % 2)
- What is printed? python print(str(3) + str(4) == '34', 3 + 4 == 7, '3' + '4' == 7)
- What is printed? python print(len(''), len(' '), len('t'), ''.isspace())
- What is printed? python print(1 + '1' if False else '1' + str(1), '3' 2 + str(4 - 1), int('3' 2) + 4 - 1)
- What is printed? python print(bool(0 or '' or []), bool([[]]), bool([0]), bool(' '.strip()))
- What is printed? python print(round(2.345, 2), round(2.355, 2), round(7.5), round(8.5))