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

Composite formatting in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Composite Formatting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of composite formatting with multiple placeholders
What is the output of the following C# code using composite formatting?
C Sharp (C#)
string result = string.Format("{0} scored {1} out of {2}.", "Alice", 85, 100);
Console.WriteLine(result);
AAlice scored 85 out of {2}.
BAlice scored 85 out of 100.
C0 scored 85 out of 100.
DAlice scored {1} out of {2}.
Attempts:
2 left
💡 Hint
Look at how placeholders {0}, {1}, and {2} are replaced by the arguments in order.
Predict Output
intermediate
2:00remaining
Composite formatting with alignment and padding
What is the output of this C# code using composite formatting with alignment?
C Sharp (C#)
string result = string.Format("|{0,10}|{1,-5}|", 42, "Hi");
Console.WriteLine(result);
A| 42|Hi |
B|42 | Hi|
C|42|Hi|
D| 42| Hi|
Attempts:
2 left
💡 Hint
Positive alignment pads left side with spaces, negative pads right side.
🔧 Debug
advanced
2:00remaining
Identify the error in composite formatting
What error does this C# code produce when run?
C Sharp (C#)
string result = string.Format("{0} {1} {2}", "one", "two");
Console.WriteLine(result);
ASystem.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
BNo error, output: one two {2}
COutput: one two
DOutput: one two {2}
Attempts:
2 left
💡 Hint
Check if the number of placeholders matches the number of arguments.
🧠 Conceptual
advanced
2:00remaining
Understanding format specifiers in composite formatting
What is the output of this C# code using composite formatting with a format specifier?
C Sharp (C#)
double pi = 3.14159;
string result = string.Format("Pi rounded: {0:F2}", pi);
Console.WriteLine(result);
APi rounded: F2
BPi rounded: 3.14159
CPi rounded: 3
DPi rounded: 3.14
Attempts:
2 left
💡 Hint
The format specifier F2 means fixed-point with 2 decimal places.
Predict Output
expert
2:00remaining
Composite formatting with nested braces and escaped characters
What is the output of this C# code using composite formatting with escaped braces?
C Sharp (C#)
string result = string.Format("{{0}} is not a placeholder, but {0} is.", 123);
Console.WriteLine(result);
A{0} is not a placeholder, but {0} is.
B0 is not a placeholder, but 123 is.
C{0} is not a placeholder, but 123 is.
DSystem.FormatException
Attempts:
2 left
💡 Hint
Double braces {{ and }} are used to escape braces in composite formatting.