ProgrammingVery hardProduct-company MCQ pattern
What is printed on the second line?
What is printed on the second line?
items = [3, -1, 2, -3, 1] print(sorted(items, key=abs)) print(sorted(items, key=abs, reverse=True)) print(sorted(items, key=lambda x: (abs(x), x)))
- A. [3, 2, -3, -1, 1]
- B. [3, -3, 2, -1, 1]
- C. [3, -3, 1, -1, 2]
- D. [-1, 1, 2, 3, -3]
Correct answer: B. [3, -3, 2, -1, 1]
With reverse=True, sorted keeps equal-key items in their original order, so 3 comes before -3 and -1 before 1. The trap is assuming reverse flips the whole stable order.
More Python Programming questions
- What is printed on the second line? python data = [('b', 2), ('a', 2), ('c', 1), ('a', 1)] print(sorted(data, key=lambda t: (t[1], t[0])…
- What is printed on the second line? python words = ['bb', 'a', 'ccc', 'dd', 'e'] print(sorted(words, key=len)) print(sorted(words…
- What is printed on the last line? python nums = ['10', '9', '100', '1'] print(sorted(nums)) print(sorted(nums, key=int)) print(max(nums)…
- What is printed on the third line? python print(sorted([(1, 'x'), (1, 'X'), (0, 'z')], key=lambda t: t[1].lower())) print(sorted(['b'…
- 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…