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()))
- A. 'first line ' 'second' 2
- B. 'first line' 'third' 2
- C. 'first line ' 'second' 3
- 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
- What is printed? python from collections import deque q = deque() for ch in 'abcd': q.append(ch) if len(q) > 2: q.popleft()…
- What is printed? python a = [1, 2, 3, 4, 5, 6, 7, 8] print(a[::3], a[2::-1], a[-3::2])
- What is printed? python x = [i for i in range(3)] g = (i for i in range(3)) print(x == list(g), list(g), len(x))
- What is printed? python print(sorted(['b10', 'b9', 'a100'], key=lambda s: (s[0], int(s[1:]))))
- What is printed? python print(round(3.14159, 3), round(12345, -2), round(0.5), round(1.5), round(2.5, 0))
- What is printed? python a = [3, 1, 2] b = sorted(a, reverse=True)[:2] c = sorted(a)[-2:] print(b, c, sum(b) + sum(c))