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

String interpolation and formatting in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Interpolation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
ASum of a and b is a + b
BSum of 5 and 3 is 8
CSum of 5 and 3 is a + b
DSum of 5 and 3 is 53
Attempts:
2 left
💡 Hint
Remember that expressions inside { } in interpolated strings are evaluated.
Predict Output
intermediate
2: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);
APi rounded to 2 decimals: 3.14
BPi rounded to 2 decimals: 3.14159
CPi rounded to 2 decimals: 3.1
DPi rounded to 2 decimals: 3
Attempts:
2 left
💡 Hint
The :F2 format specifier rounds to 2 decimal places.
Predict Output
advanced
2: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);
A|Bob|Bob|
B|Bob | Bob|
C| Bob| Bob|
D| Bob|Bob |
Attempts:
2 left
💡 Hint
Positive numbers align right, negative numbers align left in interpolation.
Predict Output
advanced
2: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);
ANumber with leading zeros: 42000
BNumber with leading zeros: 42
CNumber with leading zeros: 00042
DNumber with leading zeros: 0000042
Attempts:
2 left
💡 Hint
The D5 format pads the number with zeros to make it 5 digits long.
🧠 Conceptual
expert
2:00remaining
Understanding escape sequences in interpolated strings
Which option correctly shows how to include a literal brace character '{' in a C# interpolated string?
Astring s = $"This is a brace: {{";
Bstring s = $"This is a brace: {";
Cstring s = $"This is a brace: \{";
Dstring s = $"This is a brace: '{'";
Attempts:
2 left
💡 Hint
In interpolated strings, double braces {{ or }} are used to escape braces.