Challenge - 5 Problems
Composite Formatting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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);
Attempts:
2 left
💡 Hint
Look at how placeholders {0}, {1}, and {2} are replaced by the arguments in order.
✗ Incorrect
Composite formatting replaces placeholders {0}, {1}, {2} with the first, second, and third arguments respectively. So {0} becomes "Alice", {1} becomes 85, and {2} becomes 100.
❓ Predict Output
intermediate2: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);
Attempts:
2 left
💡 Hint
Positive alignment pads left side with spaces, negative pads right side.
✗ Incorrect
The first placeholder {0,10} pads the number 42 to the left with spaces to fill 10 characters. The second {1,-5} pads "Hi" to the right with spaces to fill 5 characters. So the output is "| 42|Hi |".
🔧 Debug
advanced2: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);
Attempts:
2 left
💡 Hint
Check if the number of placeholders matches the number of arguments.
✗ Incorrect
The format string expects three arguments for placeholders {0}, {1}, and {2}, but only two arguments are provided. This causes a System.FormatException at runtime.
🧠 Conceptual
advanced2: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);
Attempts:
2 left
💡 Hint
The format specifier F2 means fixed-point with 2 decimal places.
✗ Incorrect
The format specifier F2 rounds the number to 2 decimal places, so 3.14159 becomes 3.14 in the output.
❓ Predict Output
expert2: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);
Attempts:
2 left
💡 Hint
Double braces {{ and }} are used to escape braces in composite formatting.
✗ Incorrect
Double braces {{ and }} output a single brace character. So "{{0}}" outputs "{0}" literally, while {0} is replaced by 123.