0
0
C Sharp (C#)programming~20 mins

String comparison and equality in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
String Comparison Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this string equality check?
Consider the following C# code snippet. What will be printed to the console?
C Sharp (C#)
string a = "Hello";
string b = "hello";
Console.WriteLine(a == b);
AFalse
BTrue
CCompilation error
DRuntime exception
Attempts:
2 left
πŸ’‘ Hint
Remember that string equality with == is case-sensitive by default.
❓ Predict Output
intermediate
2:00remaining
What does this code print when comparing strings ignoring case?
What will be the output of this C# code?
C Sharp (C#)
string a = "Test";
string b = "test";
bool result = string.Equals(a, b, StringComparison.OrdinalIgnoreCase);
Console.WriteLine(result);
ACompilation error
BFalse
CTrue
DRuntime exception
Attempts:
2 left
πŸ’‘ Hint
Check the StringComparison option used in string.Equals.
❓ Predict Output
advanced
2:00remaining
What is the output of this string reference comparison?
Analyze the following C# code and determine what it prints.
C Sharp (C#)
string s1 = "hello";
string s2 = new string(new char[] {'h','e','l','l','o'});
Console.WriteLine(object.ReferenceEquals(s1, s2));
AFalse
BCompilation error
CRuntime exception
DTrue
Attempts:
2 left
πŸ’‘ Hint
ReferenceEquals checks if both variables point to the same object in memory.
❓ Predict Output
advanced
2:00remaining
What does this code print when comparing strings with Compare method?
What will be the output of this C# code snippet?
C Sharp (C#)
string a = "apple";
string b = "banana";
int result = string.Compare(a, b, StringComparison.Ordinal);
Console.WriteLine(result);
ACompilation error
BZero
CA positive number
DA negative number
Attempts:
2 left
πŸ’‘ Hint
Compare returns a negative number if the first string is less than the second.
❓ Predict Output
expert
2:00remaining
What is the output of this culture-sensitive string comparison?
Consider this C# code. What will it print?
C Sharp (C#)
using System.Globalization;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("de-DE");

string s1 = "straße";
string s2 = "strasse";
bool result = string.Equals(s1, s2, StringComparison.CurrentCultureIgnoreCase);
Console.WriteLine(result);
AFalse
BTrue
CCompilation error
DRuntime exception
Attempts:
2 left
πŸ’‘ Hint
In German culture, "ß" is considered equivalent to "ss" in comparisons.