Challenge - 5 Problems
String Interpolation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of simple string interpolation
What is the output of this C# code using string interpolation?
C Sharp (C#)
int apples = 5; int oranges = 3; string result = $"I have {apples} apples and {oranges} oranges."; Console.WriteLine(result);
Attempts:
2 left
💡 Hint
Look at how variables inside curly braces are replaced by their values.
✗ Incorrect
The $ symbol before the string allows variables inside { } to be replaced by their values. So {apples} becomes 5 and {oranges} becomes 3.
❓ Predict Output
intermediate2:00remaining
Output with expression inside interpolation
What will this C# code print?
C Sharp (C#)
int x = 4; int y = 7; Console.WriteLine($"Sum is {x + y}");
Attempts:
2 left
💡 Hint
Inside { }, you can put expressions, not just variables.
✗ Incorrect
The expression x + y is evaluated to 11 and inserted into the string.
❓ Predict Output
advanced2:00remaining
Output with format specifier in interpolation
What is the output of this C# code?
C Sharp (C#)
double price = 1234.5678; Console.WriteLine($"Price: {price:F2}");
Attempts:
2 left
💡 Hint
The :F2 means format the number with 2 decimal places.
✗ Incorrect
The format specifier F2 rounds the number to 2 decimal places.
❓ Predict Output
advanced2:00remaining
Output with nested interpolation and escape sequences
What will this C# code print?
C Sharp (C#)
string name = "Anna"; int age = 30; Console.WriteLine($"Name: {name}, Age: {age}\nWelcome!");
Attempts:
2 left
💡 Hint
The \n is a newline character and will create a line break.
✗ Incorrect
The \n creates a new line after Age: 30, so Welcome! appears on the next line.
🧠 Conceptual
expert2:30remaining
Understanding string interpolation with null values
What is the output of this C# code snippet?
C Sharp (C#)
string? city = null; Console.WriteLine($"City: {city ?? "Unknown"}");
Attempts:
2 left
💡 Hint
The ?? operator returns the right side if the left side is null.
✗ Incorrect
Since city is null, the expression city ?? "Unknown" evaluates to "Unknown".