Bird
0
0

What will be the output of the following code if example.txt contains the text "Hello"?

medium📝 Predict Output Q13 of 15
Python - File Handling Fundamentals
What will be the output of the following code if example.txt contains the text "Hello"?
with open('example.txt', 'w') as f:
    f.write('World')

with open('example.txt', 'r') as f:
    print(f.read())
AWorld
BHello
CHelloWorld
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand mode "w" effect on file content

    Opening with "w" overwrites the file, so "World" replaces "Hello".
  2. Step 2: Reading the file after writing

    Reading the file after writing will output the new content "World".
  3. Final Answer:

    World -> Option A
  4. Quick Check:

    Write mode overwrites content = "World" [OK]
Quick Trick: Write mode "w" replaces file content [OK]
Common Mistakes:
  • Assuming "w" appends instead of overwrites
  • Expecting original content to remain
  • Thinking reading causes error after writing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes