Bird
Raised Fist0

What will be the output of the following code if test.txt initially contains "Python"?

medium📝 Predict Output Q4 of Q15
Python - File Handling Fundamentals
What will be the output of the following code if test.txt initially contains "Python"?

with open("test.txt", "a") as f:
    f.write(" Rocks")
with open("test.txt", "r") as f:
    print(f.read())
A"Python Rocks"
B" RocksPython"
C"Python"
DError because file is opened twice
Step-by-Step Solution
Solution:
  1. Step 1: Understand "a" mode effect

    "a" appends " Rocks" to the end of existing content "Python".
  2. Step 2: Reading file content

    Reading after append shows combined string "Python Rocks".
  3. Final Answer:

    "Python Rocks" -> Option A
  4. Quick Check:

    Append mode adds text at end = "Python Rocks" [OK]
Quick Trick: Append mode adds text at file end [OK]
Common Mistakes:
MISTAKES
  • Thinking append adds at start
  • Assuming file open twice causes error
  • Expecting original content unchanged

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes