ProgrammingModerateProduct-company MCQ pattern
What is printed?
What is printed?
print(sorted([3, -1, 2, -5], key=abs, reverse=True))
- A. [5, 3, 2, 1]
- B. [-1, 2, 3, -5]
- C. [-5, 3, 2, -1]
- D. [3, 2, -1, -5]
Correct answer: C. [-5, 3, 2, -1]
key=abs sorts by absolute value (5, 3, 2, 1 in descending order) but keeps the original elements, so the signs stay: [-5, 3, 2, -1].
More Python Programming questions
- What is printed? python print(list(filter(None, [0, 1, "", "a", [], [0], None])))
- What is printed? python def gen(): yield 1 return 5 yield 2 print(list(gen()))
- What is printed? python print("a,b,,c".split(","), len("a b c".split()))
- What is printed? python print(True + True 3, type(True + 1).name)
- What is printed? python a = [1, 2, 3, 4, 5] a[1:3] = [9] a[::2] = [0, 0] print(a)
- What is printed? python d = {} d[1] = 'a' d[1.0] = 'b' d[True] = 'c' print(d)