ProgrammingModerateGATE CS · Product-company placement

What is printed?

What is printed?
nums = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda n: n % 2 == 0, nums))
total = sum(n * n for n in nums if n % 2)
print(evens, total, list(map(lambda a, b: a - b, nums, evens)))
  1. A. [2, 4, 6] 56 [-1, -2, -3]
  2. B. [2, 4, 6] 35 [-1, -2, -3, 4, 5, 6]
  3. C. [1, 3, 5] 35 [-1, -2, -3]
  4. D. [2, 4, 6] 35 [-1, -2, -3]

Correct answer: D. [2, 4, 6] 35 [-1, -2, -3]

filter keeps the even numbers. The odd squares 1 + 9 + 25 = 35. map with two iterables stops at the shorter one, pairing (1,2), (2,4), (3,6).

More Python Programming questions

Exams that ask this topic