A text file named message.txt contains the following lines: Good morning. I…

2026

A text file named message.txt contains the following lines:
Good morning.
I will be there on time.
Consider the following Python code:
F = open('message.txt')
S1 = F.read(3)
S2 = F.readline(4)
S3 = F.read(4)
print(S1, S3, sep='@')
Which of the following will be the correct output of the above Python code?

  1. A.

    Goo@rnin

  2. B.

    Goo@I wi

  3. C.

    ValueError

  4. D.

    EOFError

Attempted by 63 students.

Show answer & explanation

Correct answer: A

File content:
Good morning.\n
I will be there on time.

  1. S1 = F.read(3)
    Reads first 3 characters → "Goo"

Pointer now at: d (in "Good...")

  1. S2 = F.readline(4)
    Reads up to 4 characters from current position → "d mo"
    (Not printed, but pointer moves ahead)

Pointer now at: r (in "morning")

  1. S3 = F.read(4)
    Reads next 4 characters → "rnin"

Final print:
print(S1, S3, sep='@') → Goo@rnin

Explore the full course: Rssb Senior Computer Instructor