Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q13 of 15
Python - Standard Library Usage
What will be the output of this code?
with open('test.txt', 'w') as f:
    f.write('Hello')

with open('test.txt', 'a') as f:
    f.write(' World')

with open('test.txt', 'r') as f:
    print(f.read())
AError: file not found
BHello
CHello World
D World
Step-by-Step Solution
Solution:
  1. Step 1: Write 'Hello' to the file

    The first block opens 'test.txt' in write mode, which creates or clears the file, then writes 'Hello'.
  2. Step 2: Append ' World' to the file

    The second block opens the file in append mode and adds ' World' after 'Hello'.
  3. Step 3: Read and print the file content

    The last block reads the full content, which is 'Hello World', and prints it.
  4. Final Answer:

    Hello World -> Option C
  5. Quick Check:

    Write + append = 'Hello World' [OK]
Quick Trick: Write clears file, append adds to end [OK]
Common Mistakes:
  • Expecting append to overwrite
  • Not closing files before reading
  • Confusing write and append modes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes