Bird
0
0

What will be the output of this C# code?

medium📝 Predict Output Q4 of 15
C Sharp (C#) - File IO
What will be the output of this C# code?
using (var writer = new StreamWriter("log.txt"))
{
    writer.WriteLine("Hello");
}
Console.WriteLine(File.ReadAllText("log.txt"));
ACompilation error
BHello
CNo output, file is empty
DRuntime exception
Step-by-Step Solution
Solution:
  1. Step 1: Understand StreamWriter inside using block

    The writer writes "Hello" to the file and is disposed (closed) after the block.
  2. Step 2: Reading file content after using block

    File.ReadAllText reads the full content, which includes "Hello".
  3. Final Answer:

    Hello -> Option B
  4. Quick Check:

    using closes writer so file has content [OK]
Quick Trick: Using flushes and closes stream before reading file [OK]
Common Mistakes:
MISTAKES
  • Assuming file is empty because writer not flushed
  • Expecting compilation error due to using
  • Thinking runtime error occurs reading file

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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