ProgrammingVery hardProduct company OA
What is printed?
What is printed?
vals = iter([3, 1, 0, 4, 0]) print(list(iter(lambda: next(vals), 0)), list(vals))
- A. [3, 1, 0, 4] [0]
- B. [3, 1] [4, 0]
- C. [3, 1] [0, 4, 0]
- D. [3, 1, 4] []
Correct answer: B. [3, 1] [4, 0]
iter(callable, sentinel) calls the function repeatedly and stops as soon as it returns the sentinel, without including it. The lambda pulls 3, 1, then 0, which stops the outer list at [3, 1]. That 0 was already consumed from vals, so the remaining items are [4, 0] — the second 0 is just data.
More Python Programming questions
- What is printed? python class Acc: def init(self): self.items = [] def iadd(self, v): self.items.append(v) a = Acc() b = a a += 1 print(a…
- What is printed? python def f(): log = [] try: log.append("t") raise ValueError("v") except ValueError as e: log.append("e") raise…
- What is printed? python it = iter(range(10)) print(5 in it, next(it), 2 in it, list(it), 9 in range(10))
- What is printed? python calls = [] def chk(x): calls.append(x) return x > 2 r1 = any(chk(x) for x in [1, 3, 5]) r2 = all([chk(x) for x in…
- What is printed? python a = [1, 2] b = [a] 3 a = 2 b[0].append(9) print(b[2], len(b))
- What is printed? python a = [1, 2] b = [a] 3 a = a 2 b[0].append(9) print(a, b[1])