KnowledgeBoat Logo

Class - 12 CBSE Computer Science Important Output Questions 2025

What is the output of the following code fragment? Explain.

fout = open("output.txt", 'w') 
fout.write("Hello, world! \n") 
fout.write("How are you?") 
fout.close()
f = open("output.txt")
print(f.read())

Python

Python Data Handling

2 Likes

Answer

Hello, world! 
How are you?

The code first opens a file named 'output.txt' in write mode ('w'), writes the strings 'Hello, world!' and 'How are you?' to the file with a newline character in between, creating a new line between the two strings in the file. It then closes the file and reopens it again in read mode (default mode). Finally, it reads the entire content of the file and prints it.

Answered By

1 Like