Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q13 of 15
Python - Context Managers

What will be the output of this code?

with open('test.txt', 'w') as f:
    f.write('Hello')
print(f.closed)
ATrue
BFalse
CHello
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand with block effect on file

    The with block opens the file and closes it automatically after the block ends.
  2. Step 2: Check f.closed after block

    Since the file is closed after the block, f.closed will be True.
  3. Final Answer:

    True -> Option A
  4. Quick Check:

    File closed after with block = True [OK]
Quick Trick: File is closed after with block ends [OK]
Common Mistakes:
  • Thinking file stays open after with
  • Expecting file content printed
  • Confusing f.closed value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes