ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
s = "abc"
it = iter(s)
print(next(it), "".join(it), list(it), [c for c in s if c in "ab" or c == "z"])
  1. A. a abc [] ['a', 'b']
  2. B. a bc ['b', 'c'] ['a', 'b']
  3. C. a bc [] ['a', 'b', 'z']
  4. D. a bc [] ['a', 'b']

Correct answer: D. a bc [] ['a', 'b']

join consumes the rest of the iterator after next() took 'a', so list(it) is then empty. The comprehension filters the original string.

More Python Programming questions

Exams that ask this topic