Composite formatting is a way to create formatted strings by embedding placeholders like {0}, {1}, etc., in a string, which are replaced by values provided as arguments.
Click to reveal answer
beginner
How do you write a placeholder for the second argument in composite formatting?
You write it as {1} because placeholders start counting from zero, so {0} is the first argument, {1} is the second, and so on.
Click to reveal answer
beginner
What will this code output?
Console.WriteLine("Hello, {0}! Today is {1}.", "Alice", "Monday");
It will output: Hello, Alice! Today is Monday.
Click to reveal answer
intermediate
How can you format a number to show two decimal places using composite formatting?
Use a format specifier inside the placeholder like {0:F2}, where F2 means fixed-point with 2 decimals. Example:
Console.WriteLine("Price: {0:F2}", 3.14159);
outputs "Price: 3.14".
Click to reveal answer
intermediate
What happens if you provide fewer arguments than placeholders in composite formatting?
You get a runtime error (FormatException) because the placeholders expect matching arguments to replace them.
Click to reveal answer
In composite formatting, what does {0} represent?
AThe first argument to replace in the string
BThe second argument to replace in the string
CA literal zero character
DA format specifier
✗ Incorrect
{0} is the placeholder for the first argument passed after the format string.
Placeholder {1} is replaced by "Bob" and {0} by 10, so it prints "Bob scored 10 points."
How do you format a date using composite formatting to show only the year?
A{0:yyyy}
B{0:year}
C{0:Y}
D{0:yy}
✗ Incorrect
The format specifier yyyy shows the full year in four digits.
What error occurs if placeholders exceed the number of arguments?
ANullReferenceException
BFormatException
CIndexOutOfRangeException
DNo error, placeholders are ignored
✗ Incorrect
FormatException is thrown when placeholders do not have matching arguments.
Which method uses composite formatting in C#?
AConsole.WriteLine(string, params object[])
BConsole.ReadLine()
Cstring.Concat()
DConsole.Clear()
✗ Incorrect
Console.WriteLine can take a format string with placeholders and arguments to format the output.
Explain how composite formatting works in C# and give an example.
Think about how you replace parts of a string with values.
You got /4 concepts.
What are common mistakes when using composite formatting and how can you avoid them?
Consider what happens if you forget an argument or use wrong placeholder numbers.
You got /4 concepts.
Practice
(1/5)
1. What does composite formatting in C# allow you to do?
easy
A. Create new data types
B. Mix text and variable values in one string easily
C. Write comments inside strings
D. Declare variables without types
Solution
Step 1: Understand composite formatting purpose
Composite formatting lets you combine text and values in one string using placeholders.
Step 2: Identify correct description
Only Mix text and variable values in one string easily describes mixing text and values easily, which is the main use of composite formatting.
Final Answer:
Mix text and variable values in one string easily -> Option B
Quick Check:
Composite formatting = mix text and values [OK]
Hint: Remember: curly braces hold values inside strings [OK]
Common Mistakes:
Thinking composite formatting creates new types
Confusing comments with formatting
Assuming it changes variable declarations
2. Which of the following is the correct syntax for composite formatting in C#?
easy
A. Console.WriteLine("Hello {0}", name);
B. Console.WriteLine("Hello %0", name);
C. Console.WriteLine("Hello $0", name);
D. Console.WriteLine("Hello #0", name);
Solution
Step 1: Recall composite formatting syntax
Composite formatting uses curly braces with an index number like {0} to insert values.
Step 2: Match syntax to options
Only Console.WriteLine("Hello {0}", name); uses curly braces and index correctly: "Hello {0}".
Final Answer:
Console.WriteLine("Hello {0}", name); -> Option A
Quick Check:
Curly braces + index = correct syntax [OK]
Hint: Look for curly braces with numbers inside [OK]
Common Mistakes:
Using % or $ instead of curly braces
Forgetting the index number inside braces
Placing variables inside the string directly
3. What is the output of this code?
int x = 5; double y = 3.14159; Console.WriteLine("X = {0}, Y = {1:F2}", x, y);
medium
A. X = 5, Y = 3.1
B. X = 5, Y = 3.14159
C. X = {0}, Y = {1:F2}
D. X = 5, Y = 3.14
Solution
Step 1: Understand format specifier {1:F2}
The specifier F2 formats the double to 2 decimal places, so 3.14159 becomes 3.14.
Step 2: Substitute values into placeholders
{0} is replaced by 5, {1:F2} by 3.14, so output is "X = 5, Y = 3.14".
Final Answer:
X = 5, Y = 3.14 -> Option D
Quick Check:
Format specifier F2 rounds to 2 decimals [OK]
Hint: F2 means 2 decimals for numbers [OK]
Common Mistakes:
Ignoring format specifier and printing full double
Printing placeholders literally
Rounding incorrectly to 1 decimal
4. Identify the error in this composite formatting code:
int a = 10; int b = 20; Console.WriteLine("Values: {0} and {2}", a, b);
medium
A. Index 2 is out of range for arguments
B. Missing semicolon after WriteLine
C. Variables a and b are not declared
D. Curly braces should be square brackets
Solution
Step 1: Check placeholders and arguments count
Placeholders {0} and {2} are used, but only two arguments (a and b) exist at indexes 0 and 1.
Step 2: Identify invalid index
Index 2 does not exist in arguments, causing an error.
Final Answer:
Index 2 is out of range for arguments -> Option A
Quick Check:
Placeholder index must match argument count [OK]
Hint: Check if placeholder indexes match argument count [OK]
Common Mistakes:
Using placeholder index beyond arguments
Confusing syntax errors with formatting errors
Assuming curly braces can be replaced
5. You want to format a date and a number in one string like this: "Date: 2024-06-15, Price: $123.46" Which composite formatting code produces this output?
DateTime d = new DateTime(2024, 6, 15); double price = 123.4567; Console.WriteLine(???);
hard
A. "Date: {0}, Price: {1}", d.ToShortDateString(), price
B. "Date: {0:MM/dd/yyyy}, Price: ${1:F2}", d, price
C. "Date: {0:yyyy-MM-dd}, Price: ${1:F2}", d, price
D. "Date: {0:yyyy-MM-dd}, Price: {1:F2}", d, price
Solution
Step 1: Match date format to output
The output date is "2024-06-15", which matches format "yyyy-MM-dd" used in "Date: {0:yyyy-MM-dd}, Price: ${1:F2}", d, price and D.
Step 2: Match price format to output
Price is "$123.46" (rounded to 2 decimals, with $ sign). "Date: {0:yyyy-MM-dd}, Price: ${1:F2}", d, price uses ${1:F2} which prints $ and 2 decimals exactly. D uses {1:F2} which formats to 2 decimals but lacks the $ sign.
Step 3: Choose exact match
"Date: {0:yyyy-MM-dd}, Price: ${1:F2}", d, price matches the exact output format given.
Final Answer:
"Date: {0:yyyy-MM-dd}, Price: ${1:F2}", d, price -> Option C
Quick Check:
Date format + F2 for decimals = exact output [OK]
Hint: Use yyyy-MM-dd for date and F2 for 2 decimals [OK]
Common Mistakes:
Using wrong date format like MM/dd/yyyy
Using currency format {1:C} without controlling decimals
Not including $ sign explicitly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.