Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
C Sharp (C#) - Exception Handling
What will be the output of this code?
using (var sw = new System.IO.StringWriter()) {
    sw.Write("Hello");
    Console.WriteLine(sw.ToString());
}
AHello
BEmpty line
CCompilation error
DRuntime exception
Step-by-Step Solution
Solution:
  1. Step 1: Understand StringWriter behavior inside using

    The StringWriter writes "Hello" to its buffer, and ToString returns that content.
  2. Step 2: Check output before disposal

    Console.WriteLine prints the buffer content "Hello" before the using block ends and disposes the resource.
  3. Final Answer:

    Hello -> Option A
  4. Quick Check:

    using writes output before dispose = A [OK]
Quick Trick: Output appears before resource disposal inside using [OK]
Common Mistakes:
MISTAKES
  • Assuming output is empty because resource is disposed
  • Expecting compilation or runtime errors
  • Confusing disposal timing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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