ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
it = iter([1, 2, 3, 4, 5]) pairs = list(zip(it, it)) print(pairs, list(it))
- A. [(1, 2), (3, 4)] []
- B. [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)] []
- C. [(1, 2), (3, 4)] [5]
- D. [(1, 2), (3, 4), (5, None)] []
Correct answer: A. [(1, 2), (3, 4)] []
Both arguments are the SAME iterator, so zip takes alternate elements: (1, 2), (3, 4). On the third round zip pulls 5 for the first slot, then finds the iterator empty and stops – the 5 is consumed and lost, so list(it) is empty.
More Python Programming questions
- What is printed? python import copy a = [[1, 2], [3]] b = copy.copy(a) c = copy.deepcopy(a) a[0].append(9) a.append([4]) print(b, c)
- What is printed? python print(bool([]), bool([0]), bool(""), bool(" "), None == False, 0 == False)
- What is printed? python def countcalls(fn): def wrapper(a): wrapper.calls += 1 return fn(a) wrapper.calls = 0 return wrapper @countcalls…
- What is printed? python def make(): acc = [] for i in range(3): def f(x, i=i): return x i acc.append(f) acc.append(lambda x: x + i) return…
- What is printed? python class A: def go(self): return ["A"] class B(A): def go(self): return ["B"] + super().go() class C(A): def…
- What is printed? python def inner(): x = yield 1 y = yield x + 1 return x y def outer(): r = yield from inner() yield r yield from…