ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
print(sorted([-2, 1, 2, -1], key=abs))
- A. [-1, 1, 3, -2]
- B. [1, -1, 2, -3]
- C. [1, -1, -2, 2]
- D. [-2, 1, 2, -1]
Correct answer: C. [1, -1, -2, 2]
Sorting by absolute value keeps equal keys in input order. Among the values with absolute value 1, 1 comes before -1, and among those with absolute value 2, -2 comes before 2.
More Python Programming questions
- What is printed? python print('%5.1f|%-5d|' % (3.14159, 42))
- What is printed? python print(max(range(7), key=lambda i: (i % 3, i)))
- What is printed? python print(5 // 0.5, 5 % 0.5)
- What is printed? python print('xy'.replace('', '-'))
- What does this Python code print? a = [1, 2, 3] b = a a += [4] a = a + [5] print(b, a)
- What does this Python code print? it = iter([1, 2, 3, 4, 5]) print(any(x > 2 for x in it), next(it))