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

String interpolation 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 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);
AI have 5 apples and 3 oranges.
BI have {apples} apples and {oranges} oranges.
CI have apples oranges.
DI have 8 apples and oranges.
Attempts:
2 left
💡 Hint
Look at how variables inside curly braces are replaced by their values.
Predict Output
intermediate
2: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}");
ASum is 47
BSum is x + y
CSum is {x + y}
DSum is 11
Attempts:
2 left
💡 Hint
Inside { }, you can put expressions, not just variables.
Predict Output
advanced
2: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}");
APrice: 1234.5678
BPrice: 1234.57
CPrice: {price:F2}
DPrice: 1234.5
Attempts:
2 left
💡 Hint
The :F2 means format the number with 2 decimal places.
Predict Output
advanced
2: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!");
AName: Anna, Age: 30 Welcome!
BName: Anna, Age: 30\nWelcome!
C
Name: Anna, Age: 30
Welcome!
DName: {name}, Age: {age}\nWelcome!
Attempts:
2 left
💡 Hint
The \n is a newline character and will create a line break.
🧠 Conceptual
expert
2: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"}");
ACity: Unknown
BCity: {city ?? "Unknown"}
CCity: null
DCity:
Attempts:
2 left
💡 Hint
The ?? operator returns the right side if the left side is null.