ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
import sys
data = 'first line  \nsecond\n'
it = iter(data.splitlines())
print(repr(next(it)), repr(next(it).strip()), len(data.split()))
  1. A. 'first line ' 'second' 2
  2. B. 'first line' 'third' 2
  3. C. 'first line ' 'second' 3
  4. D. 'first line' 'second' 4

Correct answer: C. 'first line ' 'second' 3

splitlines removes the newline but keeps the trailing spaces on the first line, so repr keeps them. strip removes the spaces on the second line. split() with no argument counts three words: first, line and second.

More Python Programming questions

Exams that ask this topic