Skip to main contentSkip to solution

Given data in text file "rhymes.txt" as follows:

Johny Johny Yes Papa

Eating Sugar No Papa

Telling a lie No Papa

Open your mouth Ha Ha Ha

Find the output for the following code:

my1=open("rhymes.txt","r")

data=my1.read(10)

print(data)

my1.close()

Solution

✅ Correct Option: 3

read(10) reads the first 10 characters of the file, counting spaces too, and not 10 words.

CharactersCount
J o h n y5
(space)6
J o h n10

So the output is Johny John.


Option 4: "Johny Johny" has 11 characters, so it needs read(11).

Option 1: this would need read() with no argument, which returns the whole file, and even then it would not stop at "Telling a".

Option 2: read() returns a plain string, so no square brackets are printed.

readline() reads one line and readlines() returns a list of lines, but read(n) always reads exactly n characters.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question