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

String concatenation behavior in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Concatenation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
AAge: 205
B25
CCompilation error
DAge: 25
Attempts:
2 left
💡 Hint
Remember how C# evaluates string concatenation with numbers from left to right.
Predict Output
intermediate
2: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);
AAge: 25
BAge: 205
C25
DCompilation error
Attempts:
2 left
💡 Hint
Parentheses change the order of operations.
Predict Output
advanced
2: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);
Anull
BCompilation error
CHello
DnullHello
Attempts:
2 left
💡 Hint
Check how null strings behave when concatenated with non-null strings.
Predict Output
advanced
2: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);
AHi there
BRuntime error
CCompilation error
DSystem.Text.StringBuilder there
Attempts:
2 left
💡 Hint
Consider how + operator works with StringBuilder and string.
Predict Output
expert
3: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);
ACompilation error
BHello World
CHelloWorld
DRuntime error
Attempts:
2 left
💡 Hint
Look at how the + operator is overloaded in the custom class.