Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
Python - File Handling Fundamentals
What will be the output of this code?
with open('test.txt', 'w') as f:
    f.write('Hello')
with open('test.txt', 'r') as f:
    print(f.read())
Atest.txt
BHello
CError: file not found
DEmpty output
Step-by-Step Solution
Solution:
  1. Step 1: Write 'Hello' to the file

    The first block opens 'test.txt' in write mode and writes 'Hello' to it.
  2. Step 2: Read and print the file content

    The second block opens the same file in read mode and prints its content, which is 'Hello'.
  3. Final Answer:

    Hello -> Option B
  4. Quick Check:

    File write and read output = Hello [OK]
Quick Trick: Write then read file to see saved content [OK]
Common Mistakes:
  • Expecting filename instead of content
  • Thinking file is empty after writing
  • Assuming file not found error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes