ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
nums = [1, 2, 3] doubled = list(map(lambda x: x * 2, nums)) evens = list(filter(lambda x: x % 2 == 0, nums)) print(doubled, evens)
- A. [2, 4, 6] [1, 3]
- B. [1, 2, 3] [2]
- C. [2, 4, 6] [2]
- D. [2, 4, 6] [1, 2, 3]
Correct answer: C. [2, 4, 6] [2]
map applies the doubling lambda to every element, giving [2,4,6]. filter keeps only elements where the lambda returns truthy (even numbers), keeping just 2 from the original list.
More Python Programming questions
- What is printed? python class A: def call(self, x): return x 2 a = A() print(a(5), callable(a))
- What is printed? python class Node: def init(self, val, children=None): self.val = val self.children = children if children is not None…
- What does the following Python 3 code print? lst = [x x for x in range(5)] gen = (x x for x in range(5)) print(type(lst), type(gen))
- What does the following Python 3 code print? a = [1, 2, 3] b = a b.append(4) print(a)
- What does the following Python 3 code print? a = [1, 2, 3] b = a[:] b.append(4) print(a, b)
- What does the following Python 3 code print? def f(a, b, args): return a, b, args print(f(1, 2, 3, 4, 5))