Challenge - 5 Problems
String Concatenation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of concatenating strings and integers
What is the output of the following C# code?
C Sharp (C#)
string result = "Age: " + 20 + 5; Console.WriteLine(result);
Attempts:
2 left
💡 Hint
Remember how C# evaluates string concatenation with numbers from left to right.
✗ Incorrect
In C#, when concatenating strings and numbers, the expression is evaluated left to right. "Age: " + 20 becomes "Age: 20" (string), then + 5 concatenates "5" resulting in "Age: 205".
❓ Predict Output
intermediate2:00remaining
Concatenation with parentheses changing order
What is the output of this C# code snippet?
C Sharp (C#)
string result = "Age: " + (20 + 5); Console.WriteLine(result);
Attempts:
2 left
💡 Hint
Parentheses change the order of operations.
✗ Incorrect
The parentheses cause 20 + 5 to be evaluated first as integer addition (25), then concatenated with the string "Age: ", resulting in "Age: 25".
❓ Predict Output
advanced2:00remaining
Concatenation with null strings
What will be printed by this C# code?
C Sharp (C#)
string s1 = null; string s2 = "Hello"; string result = s1 + s2; Console.WriteLine(result == null ? "null" : result);
Attempts:
2 left
💡 Hint
Check how null strings behave when concatenated with non-null strings.
✗ Incorrect
In C#, null strings are treated as empty strings when concatenated. So null + "Hello" results in "Hello".
❓ Predict Output
advanced2:00remaining
Concatenation with StringBuilder and + operator
What is the output of this C# code?
C Sharp (C#)
var sb = new System.Text.StringBuilder(); sb.Append("Hi"); string result = sb + " there"; Console.WriteLine(result);
Attempts:
2 left
💡 Hint
Consider how + operator works with StringBuilder and string.
✗ Incorrect
The + operator with StringBuilder and string calls ToString() on StringBuilder, then concatenates. So sb.ToString() is "Hi", concatenated with " there" results in "Hi there". But since sb is an object, the expression sb + " there" is interpreted as object + string, which calls ToString() on sb, resulting in "Hi" + " there" = "Hi there". However, in C# + operator is not defined for StringBuilder and string, so it calls ToString() on sb. So the output is "Hi there".
❓ Predict Output
expert3:00remaining
Concatenation with overloaded + operator in custom class
Given the following class and code, what is the output?
C Sharp (C#)
public class MyString { public string Value; public MyString(string val) { Value = val; } public static string operator +(MyString a, string b) => a.Value + b; } var ms = new MyString("Hello"); string result = ms + " World"; Console.WriteLine(result);
Attempts:
2 left
💡 Hint
Look at how the + operator is overloaded in the custom class.
✗ Incorrect
The + operator is overloaded to concatenate the Value field with the string b, so ms + " World" returns "Hello World".