Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q13 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())
AEmpty output
Btest.txt
CError: file not found
DHello
Step-by-Step Solution
Solution:
  1. Step 1: Write 'Hello' to file 'test.txt'

    The first block opens 'test.txt' in write mode and writes 'Hello' inside 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 D
  4. Quick Check:

    Write then read = Hello [OK]
Quick Trick: Write then read file prints written text [OK]
Common Mistakes:
  • Expecting error because file exists
  • Thinking file is empty after writing
  • Confusing file name with content

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes