ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
it = iter([1, 2])
try:
while True:
print(next(it), end=" ")
except StopIteration as e:
print("stop", e.value, end=" ")
print(next(it, "dflt"), next(iter([]), None))- A. 1 2 stop None dflt None
- B. 1 2 stop dflt None
- C. 1 2 None dflt None
- D. 1 2 stop 0 dflt None
Correct answer: A. 1 2 stop None dflt None
An exhausted list iterator raises StopIteration whose value is None. next() with a default returns the default instead of raising, both for an exhausted and an empty iterator.
More Python Programming questions
- What is printed? python def f(): try: return [1] finally: pass def g(): x = [1] try: return x finally: x.append(2) def h(): x = 1 try…
- What is printed? (Output lines are shown separated by " / ".) python class P: def init(self, x): self.x = x def eq(self, o): return self.x…
- What is printed? python class V: def init(self, n): self.n = n def add(self, o): return V(self.n + o) def radd(self, o): return V(o 10 +…
- What is printed? python class R: def init(self, n): self.n = n def len(self): return self.n def getitem(self, i): if i >= self.n: raise…
- What is printed? python class C: def init(self): self.d = {} def getattr(self, name): return f"<{name}>" def setattr(self, name, v)…
- What is printed? python class Temp: def init(self): self.c = 0 @property def c(self): return self.c @c.setter def c(self, v): if v < -273…