Bird
0
0

Identify the error in the following code snippet:

medium📝 Debug Q6 of 15
C Sharp (C#) - Strings and StringBuilder
Identify the error in the following code snippet:
string s1 = "abc";
string s2 = null;
bool isEqual = s1.Equals(s2);
AReturns false safely
BCompilation error due to null comparison
CNullReferenceException at runtime
DReturns true incorrectly
Step-by-Step Solution
Solution:
  1. Step 1: Understand Equals with null argument

    Calling s1.Equals(s2) where s2 is null returns false safely, no exception.
  2. Step 2: Check for runtime errors

    Since s1 is not null, no NullReferenceException occurs.
  3. Final Answer:

    Returns false safely -> Option A
  4. Quick Check:

    Equals returns false when compared to null [OK]
Quick Trick: Equals returns false, not exception, when comparing to null [OK]
Common Mistakes:
MISTAKES
  • Expecting NullReferenceException
  • Thinking null causes compile error
  • Assuming Equals returns true with null

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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