Exceptions & Files · Question 15 · Medium

readline() is called twice and then read() once on a three-line file-like object. What do the last two calls return?

import io

f = io.StringIO("a\nb\nc")
f.readline()
print(repr(f.readline()), repr(f.read()))
  1. 'b' 'c'
  2. 'b\n' 'c'
  3. 'a\n' 'b\nc'
  4. 'b\n' 'a\nb\nc'
Answer and explanation

Answer: B. 'b\n' 'c'

Explanation: The first readline consumes 'a\n'. The second returns the next line with its newline kept, 'b\n'. read() then returns whatever is left from the current position, which is 'c'.

Previous: The only except clause below names TypeError, but the conversion fails in a… · Next: A function returns from inside try, and its finally block has a print. In what…

Practise exceptions & files

Try a fresh set of exceptions & files questions with an explanation after every answer, free and without signing up: Exceptions & Files practice. Or sit a full placement mock test.

More exceptions & files questions

More python practice

Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops · Functions & Scope · Comprehensions & Generators

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge