ProgrammingVery hardProduct-company MCQ pattern
What is printed on the third line?
What is printed on the third line?
print(sorted([(1, 'x'), (1, 'X'), (0, 'z')], key=lambda t: t[1].lower())) print(sorted(['b', 'A', 'a', 'B'])) print(sorted(['b', 'A', 'a', 'B'], key=str.lower))
- A. ['A', 'B', 'a', 'b']
- B. ['A', 'a', 'b', 'B']
- C. ['b', 'a', 'A', 'B']
- D. ['A', 'a', 'c', 'b']
Correct answer: B. ['A', 'a', 'b', 'B']
str.lower makes 'A' and 'a' equal keys, and the stable sort keeps their input order. The B and b keys also tie, so the original order A, a, b, B is kept.
More Python Programming questions
- What is printed on the second line? python u = '' for ch in 'abc': u = ch + u print(u)
- What is printed on the first line? python x = 0.1 3 y = 0.3 print(abs(x - y) < 1e-9, x > y, x - y, y - x) print(float('0.1') 3 == 0.3, 3…
- What is printed on the second line? python import math print(math.isclose(1e9, 1e9 + 1e-3), math.isclose(1e9, 1e9 + 1, reltol=1e-9))…
- What is printed on the first line? python print(0.1 + 0.2 - 0.3 == 0, 0.1 + 0.2 - 0.3) print(1 - 0.9 == 0.1, 1 - 0.9)
- What is printed? python import sys print(sys.floatinfo.epsilon, 1.0 + sys.floatinfo.epsilon > 1.0, 1.0 + sys.floatinfo.epsilon / 2 > 1.0)
- What is printed on the first line? python print(263 2-63 == 1.0, 264 + 1.0 - 264, (264 + 1) - 264) print(float(264 + 1) - float(264))