Bird
Raised Fist0

Identify the error in this code snippet:

medium📝 Debug Q6 of Q15
C Sharp (C#) - File IO
Identify the error in this code snippet:
File.WriteAllText("output.txt", "Hello World");
File.WriteAllText("output.txt", "New Content");
string text = File.ReadAllText("output.txt");
Console.WriteLine(text);
AFile.WriteAllText cannot be called twice on the same file
BThe second WriteAllText overwrites the first content
CFile.ReadAllText returns null if file is overwritten
DNo error; output will be "Hello WorldNew Content"
Step-by-Step Solution
Solution:
  1. Step 1: Understand WriteAllText behavior

    Each call to WriteAllText overwrites the file content completely.
  2. Step 2: Analyze the output after two writes

    The second call replaces "Hello World" with "New Content".
  3. Final Answer:

    The second WriteAllText overwrites the first content -> Option B
  4. Quick Check:

    WriteAllText overwrites file = B [OK]
Quick Trick: WriteAllText replaces file content, not appends [OK]
Common Mistakes:
MISTAKES
  • Thinking it appends content
  • Expecting null from ReadAllText
  • Believing multiple writes cause error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes