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)))
- A. [2, 4, 6] 56 [-1, -2, -3]
- B. [2, 4, 6] 35 [-1, -2, -3, 4, 5, 6]
- C. [1, 3, 5] 35 [-1, -2, -3]
- 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
- What is printed? python def chunks(xs, k): for i in range(0, len(xs), k): yield xs[i:i + k] def flatten(seq): for s in seq: if…
- What is printed? (Output lines are shown separated by " / ".) python def f(a, b, /, c, , d): return (a, b, c, d) args = (1, 2) kw = {"c"…
- What is printed? python class A: def init(self, kw): print("A", sorted(kw), end=" ") class B(A): def init(self, b=0, kw): print("B", end="…
- What is printed? python class Base: def run(self): return self.step() def step(self): return "base" class Child(Base): def step(self)…
- What is printed? python class Meta(type): def new(m, name, bases, ns): ns["kind"] = name.upper() return super().new(m, name, bases, ns)…
- What is printed? python class S: slots = ("a",) class T(S): pass t = T() t.a = 1 t.b = 2 s = S() try: s.b = 2 except AttributeError…