Bird
0
0

Find the issue in this code:

medium📝 Debug Q7 of 15
C Sharp (C#) - Strings and StringBuilder
Find the issue in this code:
string s = null;
int length = s.Length;
Console.WriteLine(length);
ACode runs without error and prints 0
BSyntax error due to null string
CLength property returns 0 for null
DNullReferenceException at runtime
Step-by-Step Solution
Solution:
  1. Step 1: Understand null string behavior

    Accessing Length on a null string causes a NullReferenceException at runtime.
  2. Step 2: Confirm no compile error

    Code compiles but fails when run due to null dereference.
  3. Final Answer:

    NullReferenceException at runtime -> Option D
  4. Quick Check:

    Accessing Length on null throws exception [OK]
Quick Trick: Check for null before accessing string properties [OK]
Common Mistakes:
MISTAKES
  • Assuming Length returns 0
  • Expecting compile error
  • Ignoring null checks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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