KnowledgeBoat Logo

Class - 12 CBSE Computer Science Important Output Questions 2025

Write the output of the following code with justification if the contents of the file "ABC.txt" are:

"Welcome to Python Programming!"

f1 = open("ABC.txt", "r")
size = len(f1.read())
print(size)
data = f1.read(5)
print(data)

Python

Python Data Handling

1 Like

Answer

30

The code opens the file 'ABC.txt' in read mode ('r') and creates a file object named f1. The next line, size = len(f1.read()), reads the entire contents of the file 'ABC.txt', returning a string containing the file's contents. Then, len() calculates the length of this string and assigns it to the variable size. After the f1.read() operation, the file pointer is positioned at the end of the file. The code then prints the size. The subsequent line, data = f1.read(5), attempts to read the first 5 characters from the file 'ABC.txt' using f1.read(5). However, since the file pointer is already at the end of the file, there are no characters left to read. Therefore, this read(5) operation will return an empty string. Finally, the code prints the value of data, which is an empty string.

Answered By

1 Like