Challenge - 5 Problems
String Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this C# string code?
Consider the following C# code snippet. What will be printed?
DSA C#
string s1 = "hello"; string s2 = "hello"; Console.WriteLine(object.ReferenceEquals(s1, s2));
Attempts:
2 left
💡 Hint
Think about how C# handles string literals in memory.
✗ Incorrect
In C#, string literals are interned, meaning identical literals share the same memory reference. So, s1 and s2 point to the same object, making ReferenceEquals return True.
❓ Predict Output
intermediateWhat is the output after modifying a string variable?
What will be printed by this C# code?
DSA C#
string s = "abc"; s = s + "d"; Console.WriteLine(s);
Attempts:
2 left
💡 Hint
Remember strings are immutable but concatenation creates a new string.
✗ Incorrect
Strings in C# are immutable, so concatenation creates a new string "abcd" and assigns it to s. The output is "abcd".
🔧 Debug
advancedWhat error does this code raise?
What error will this C# code produce when run?
DSA C#
string s = null;
int length = s.Length;
Console.WriteLine(length);Attempts:
2 left
💡 Hint
Accessing a member on a null object causes an error.
✗ Incorrect
Since s is null, accessing s.Length throws a NullReferenceException at runtime.
🧠 Conceptual
advancedHow many objects are created in memory?
How many string objects are created in memory after this code runs?
DSA C#
string a = "cat"; string b = "cat"; string c = new string(new char[] {'c','a','t'});
Attempts:
2 left
💡 Hint
Consider string interning and the new string constructor.
✗ Incorrect
"cat" literal is interned once, so a and b point to the same object. The new string created by c is a separate object. Total 2 objects.
🚀 Application
expertWhat is the output of this string manipulation code?
What will be printed by this C# code snippet?
DSA C#
string s = "hello"; char[] arr = s.ToCharArray(); arr[0] = 'j'; string s2 = new string(arr); Console.WriteLine(s); Console.WriteLine(s2);
Attempts:
2 left
💡 Hint
Strings are immutable; modifying the char array does not change the original string.
✗ Incorrect
The original string s remains "hello". The char array is modified and used to create s2 as "jello".
