Bird
0
0

What will be the output of this code snippet?

medium📝 Predict Output Q5 of 15
C Sharp (C#) - File IO
What will be the output of this code snippet?
using (var fs = new FileStream("data.txt", FileMode.Create))
using (var writer = new StreamWriter(fs))
{
    writer.WriteLine("Line1");
}
Console.WriteLine(File.ReadAllText("data.txt"));
AEmpty output
BLine1
CFileNotFoundException
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Nested using statements open and write to file

    FileStream is created, StreamWriter writes "Line1" to it.
  2. Step 2: Both streams are disposed properly

    Disposing StreamWriter flushes data, disposing FileStream closes file.
  3. Final Answer:

    Line1 -> Option B
  4. Quick Check:

    Nested using ensures file content is written [OK]
Quick Trick: Nested using disposes inner writer before outer stream [OK]
Common Mistakes:
MISTAKES
  • Assuming file is empty due to missing flush
  • Expecting exceptions from nested using
  • Confusing FileMode.Create with FileMode.Open

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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