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 string interpolation with expressions
What is the output of the following C# code?
C Sharp (C#)
int a = 5; int b = 3; string result = $"Sum of {a} and {b} is {a + b}"; Console.WriteLine(result);
Attempts:
2 left
💡 Hint
Remember that expressions inside { } in interpolated strings are evaluated.
✗ Incorrect
The variables a and b are replaced by their values 5 and 3, and the expression a + b is evaluated to 8 inside the interpolated string.
❓ Predict Output
intermediate2:00remaining
Formatting numbers with string interpolation
What will be printed by this C# code?
C Sharp (C#)
double pi = 3.14159; string formatted = $"Pi rounded to 2 decimals: {pi:F2}"; Console.WriteLine(formatted);
Attempts:
2 left
💡 Hint
The :F2 format specifier rounds to 2 decimal places.
✗ Incorrect
The format specifier F2 formats the double to 2 decimal places, so 3.14159 becomes 3.14.
❓ Predict Output
advanced2:00remaining
Aligning text with string interpolation
What is the output of this C# code snippet?
C Sharp (C#)
string name = "Bob"; string output = $"|{name,10}|{name,-10}|"; Console.WriteLine(output);
Attempts:
2 left
💡 Hint
Positive numbers align right, negative numbers align left in interpolation.
✗ Incorrect
The {name,10} aligns the string right in a 10-character field, adding spaces on the left. The {name,-10} aligns left, adding spaces on the right.
❓ Predict Output
advanced2:00remaining
Custom numeric format strings in interpolation
What will this C# code print?
C Sharp (C#)
int number = 42; string formatted = $"Number with leading zeros: {number:D5}"; Console.WriteLine(formatted);
Attempts:
2 left
💡 Hint
The D5 format pads the number with zeros to make it 5 digits long.
✗ Incorrect
The D5 format specifier means decimal with at least 5 digits, so 42 becomes 00042.
🧠 Conceptual
expert2:00remaining
Understanding escape sequences in interpolated strings
Which option correctly shows how to include a literal brace character '{' in a C# interpolated string?
Attempts:
2 left
💡 Hint
In interpolated strings, double braces {{ or }} are used to escape braces.
✗ Incorrect
To include a literal brace in an interpolated string, you must double it. So {{ outputs a single { character.