Test Index

CBSE Class 12 Computer Science 2025 Paper Compartment Get it on Google PlayDownload on the App Store

© examsnet.com
Question : 10 of 37
Marks: +1, -0
A text file song.txt contains the following contents in it :
Life goes on as it never ends
What will be the output of the following code snippet ?
f1=open("song.txt","r")
s1=f1.read(5)
s2=f1.readline(4)
s3=f1.read(3)
print(s1,s3,sep="#")
Solution:  
Concept:
read(n) reads the next n characters; readline(n) reads at most n characters of the current line. Each read continues from where the previous one stopped (the file pointer moves).
Explanation:
s1 = f1.read(5) → 'Life ' (L, i, f, e and the space)
s2 = f1.readline(4) → 'goes'
s3 = f1.read(3) → ' on' (space, o, n)
print(s1, s3, sep="#") prints only s1 and s3 with # between them:
Life # on
Key point: s2 is read but never printed, and the spaces stored inside s1 and s3 appear on both sides of #.
Answer:
Option C (Life # on)
© examsnet.com
Advertisements
Go to Question: