CBSE Class 12 Computer Science
Question 42 of 136
Data File Handling — Question 7
Back to all questionsTrue
Reason — Initially, the variable f is assigned None. The code then enters a for loop that runs five times (for i in range(5)). Inside the loop, a file named "data.txt" is opened in write mode ("w") using the with statement. If the value of i is greater than 2, the loop breaks using break. After the loop breaks, the print(f.closed) statement is executed outside the with block. Since the file object f was opened within the with block, it is automatically closed when the block exits. The f.closed attribute returns True if the file is closed, which happens because of the with statement's automatic closing behavior.