ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
it = (x * x for x in range(5)) first = next(it) rest = [v for v in it if v % 2 == 0] again = sum(1 for _ in it) print(first, rest, again)
- A. 0 [0, 4, 16] 0
- B. 0 [4, 16] 5
- C. 0 [1, 9] 0
- D. 0 [4, 16] 0
Correct answer: D. 0 [4, 16] 0
next takes the first square 0; the comprehension consumes the rest (1, 4, 9, 16), keeping the even ones. The generator is then exhausted, so the final count is 0.
More Python Programming questions
- What is printed? python def g(): for i in range(3): try: yield i finally: print(f"f{i}", end=" ") for v in g(): if v == 1: break…
- What is printed? python def deco(cls): orig = cls.init def init(self, a): orig(self, a) self.ok = True cls.init = init return cls @deco…
- What is printed? python import functools @functools.singledispatch def show(x): return "obj" @show.register def (x: int): return "int"…
- What is printed? python def f(): x = 0 def inc(): nonlocal x x += 1 return x return [inc() for in range(3)], x print(f())
- What is printed? python def f(kw): return list(kw) print(f(b=1, a=2), f(dict(z=0), y=1), f({"1": 1}))
- What is printed? python class A: def getitem(self, k): return k a = A() print(a[1], a[1:3], a[::2], a[1, 2:3], a[...])