A text file song.txt contains the following contents in it: Life goes on as it…
2025
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.read (4) s3 = f1.read (3) print(s1, s3, sep = "#")
- A.
goes # on
- B.
Life #goes # on
- C.
Life # on
- D.
Error
Attempted by 511 students.
Show answer & explanation
Correct answer: C
Step 1: The file 'song.txt' is opened in read mode using open("song.txt", "r").
Step 2: f1.read(5) reads the first 5 characters from the file, which are 'Life ' (including the space).
Step 3: f1.read(4) reads the next 4 characters, which are 'goes'.
Step 4: f1.read(3) reads the next 3 characters, which are ' on'.
Step 5: The print function outputs s1 and s3 separated by '#', so 'Life ' and ' on' are printed as 'Life # on'.
हिन्दी उत्तर:
चरण 1: फाइल 'song.txt' को रीड मोड में open("song.txt", "r") का उपयोग करके खोला जाता है।
चरण 2: f1.read(5) फाइल से पहले 5 वर्ण पढ़ता है, जो 'Life ' (स्पेस सहित) है।
चरण 3: f1.read(4) अगले 4 वर्ण पढ़ता है, जो 'goes' है।
चरण 4: f1.read(3) अगले 3 वर्ण पढ़ता है, जो ' on' है।
चरण 5: print फ़ंक्शन s1 और s3 को '#' से अलग करके आउटपुट देता है, इसलिए 'Life ' और ' on' को 'Life # on' के रूप में प्रिंट किया जाता है।