ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
a = [3, 1, 2]
b = sorted(a, reverse=True)[:2]
c = sorted(a)[-2:]
print(b, c, sum(b) + sum(c))
  1. A. [3, 2] [2, 3] 10
  2. B. [3, 1] [1, 2] 7
  3. C. [3, 2] [1, 2] 8
  4. D. [3, 2] [2, 3] 11

Correct answer: A. [3, 2] [2, 3] 10

b is the two largest values in descending order, [3, 2]. c is the two largest values in ascending order, [2, 3]. Their sums are 5 and 5, which total 10.

More Python Programming questions

Exams that ask this topic