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))
  1. A. 1 2 stop None dflt None
  2. B. 1 2 stop dflt None
  3. C. 1 2 None dflt None
  4. 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

Exams that ask this topic