Bird
0
0

Find the error in this C# code snippet:

medium📝 Debug Q6 of 15
C Sharp (C#) - Strings and StringBuilder
Find the error in this C# code snippet:
string s = null;
int len = s.Length;
Console.WriteLine(len);
ASyntax error: missing semicolon
BNullReferenceException because s is null when accessing Length
CCompile error: string cannot be null
DNo error, prints 0
Step-by-Step Solution
Solution:
  1. Step 1: Identify null string usage

    s is assigned null, so it points to no string object.
  2. Step 2: Accessing Length on null causes runtime error

    Trying to get Length property on null throws NullReferenceException.
  3. Final Answer:

    NullReferenceException because s is null when accessing Length -> Option B
  4. Quick Check:

    Accessing members on null causes NullReferenceException [OK]
Quick Trick: Never access properties on null strings without checks [OK]
Common Mistakes:
MISTAKES
  • Assuming null strings have length 0
  • Thinking compiler disallows null assignment
  • Ignoring runtime exceptions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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