Bird
Raised Fist0

Find the bug in this code:

medium📝 Debug Q7 of Q15
C Sharp (C#) - Strings and StringBuilder
Find the bug in this code:
string s1 = null;
string s2 = "hello";
bool result = s1.Equals(s2);
AReturns false safely
BCompilation error due to null variable
CNullReferenceException because s1 is null
DReturns true incorrectly
Step-by-Step Solution
Solution:
  1. Step 1: Check method call on null object

    Calling s1.Equals when s1 is null causes NullReferenceException.
  2. Step 2: Understand safe comparison alternatives

    Use string.Equals(s1, s2) static method to avoid exception.
  3. Final Answer:

    NullReferenceException because s1 is null -> Option C
  4. Quick Check:

    Calling instance method on null causes exception [OK]
Quick Trick: Avoid calling instance methods on null strings [OK]
Common Mistakes:
MISTAKES
  • Assuming Equals returns false on null instance
  • Expecting compile error instead of runtime error
  • Not using static Equals method for null safety

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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