Recall & Review
beginner
What is composite formatting in C#?
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? <br>
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: <br>
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?
✗ Incorrect
{0} is the placeholder for the first argument passed after the format string.
What will this code print? <br>
Console.WriteLine("{1} scored {0} points.", 10, "Bob");✗ Incorrect
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?
✗ Incorrect
The format specifier yyyy shows the full year in four digits.
What error occurs if placeholders exceed the number of arguments?
✗ Incorrect
FormatException is thrown when placeholders do not have matching arguments.
Which method uses composite formatting in C#?
✗ 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.