Challenge - 5 Problems
String Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this code?
Consider the following C# code snippet. What will be printed to the console?
C Sharp (C#)
string s = "hello"; s = s.Replace('h', 'j'); Console.WriteLine(s);
Attempts:
2 left
💡 Hint
Remember that strings in C# are immutable. The Replace method returns a new string but does not change the original.
✗ Incorrect
The Replace method returns a new string with the replacements, but since the returned value is not assigned, the original string 's' remains unchanged. So, 'hello' is printed.
❓ Predict Output
intermediate2:00remaining
What is the output of this code?
What will this C# program print?
C Sharp (C#)
string a = "abc"; string b = a + "def"; Console.WriteLine(a); Console.WriteLine(b);
Attempts:
2 left
💡 Hint
Adding strings creates a new string; the original string remains unchanged.
✗ Incorrect
The variable 'a' remains 'abc'. The variable 'b' is a new string 'abcdef'. So the output is 'abc' then 'abcdef'.
🔧 Debug
advanced2:00remaining
Why does this code not change the string?
This code tries to change the first character of a string. Why does it fail?
C Sharp (C#)
string s = "test"; s[0] = 'b'; Console.WriteLine(s);
Attempts:
2 left
💡 Hint
Think about whether you can assign to a character in a string by index in C#.
✗ Incorrect
Strings in C# are immutable. You cannot assign to an index of a string. This causes a compilation error.
❓ Predict Output
advanced2:00remaining
What is the output of this code?
What will this program print?
C Sharp (C#)
string s1 = "hello";
string s2 = s1.ToUpper();
Console.WriteLine(s1);
Console.WriteLine(s2);Attempts:
2 left
💡 Hint
ToUpper returns a new string; it does not change the original string.
✗ Incorrect
s1 remains 'hello' because strings are immutable. s2 is a new string 'HELLO'.
🧠 Conceptual
expert2:00remaining
Why is string immutability important in C#?
Which of the following is NOT a reason why strings are immutable in C#?
Attempts:
2 left
💡 Hint
Think about what immutability means and how it affects string modification.
✗ Incorrect
Strings being immutable means you cannot modify characters directly. This is a design choice for safety and performance, not to allow direct modification.