Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
C Sharp (C#) - Exception Handling
Identify the error in this code snippet:
using (var reader = new System.IO.StreamReader("file.txt"))
Console.WriteLine(reader.ReadLine());
Console.WriteLine(reader.ReadLine());
ANo error, code runs fine
BSecond Console.WriteLine tries to use disposed reader
CStreamReader does not implement IDisposable
DMissing braces cause only first line inside using
Step-by-Step Solution
Solution:
  1. Step 1: Check using statement block scope

    Without braces, only the first statement is inside the using block.
  2. Step 2: Second Console.WriteLine runs after disposal

    The second call uses reader after disposal, causing runtime error.
  3. Final Answer:

    Missing braces cause only first line inside using -> Option D
  4. Quick Check:

    using needs braces for multiple statements = A [OK]
Quick Trick: Use braces to include multiple statements in using block [OK]
Common Mistakes:
MISTAKES
  • Assuming both lines are inside using without braces
  • Thinking StreamReader lacks IDisposable
  • Ignoring scope rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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