Bird
0
0

What is the output of the following C# code?

medium📝 Predict Output Q13 of 15
C Sharp (C#) - Strings and StringBuilder
What is the output of the following C# code?
string s1 = "apple";
string s2 = "Banana";
int result = string.Compare(s1, s2, StringComparison.OrdinalIgnoreCase);
Console.WriteLine(result);
A-1
B0
C1
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Understand string.Compare with OrdinalIgnoreCase

    It compares strings ignoring case and returns negative if first is before second alphabetically.
  2. Step 2: Compare "apple" and "Banana" ignoring case

    "apple" comes before "banana" alphabetically, so result is negative (-1).
  3. Final Answer:

    -1 -> Option A
  4. Quick Check:

    "apple" < "Banana" ignoring case = -1 [OK]
Quick Trick: Compare returns negative if first string is alphabetically before second [OK]
Common Mistakes:
MISTAKES
  • Assuming Compare returns boolean
  • Ignoring case sensitivity in comparison
  • Expecting 0 when strings differ

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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