What does the format specifier "C" do when formatting numbers in C#?
The "C" specifier formats a number as currency, adding the currency symbol and two decimal places by default.
Click to reveal answer
beginner
How do you format a DateTime to show only the year and month in C#?
Use the format specifier "yyyy-MM" with DateTime.ToString(), which shows the full year and two-digit month.
Click to reveal answer
intermediate
What is the difference between "D" and "d" format specifiers for dates in C#?
"D" shows the long date pattern (e.g., "Monday, June 5, 2023"), while "d" shows the short date pattern (e.g., "6/5/2023").
Click to reveal answer
beginner
How does the "N" format specifier format numbers in C#?
"N" formats numbers with thousands separators and two decimal places by default (e.g., "1,234.56").
Click to reveal answer
beginner
What does the format specifier "F" do when formatting numbers?
"F" formats a number as a fixed-point number with a default of two decimal places (e.g., "1234.56"). You can specify decimals like "F3" for three decimals.
Click to reveal answer
Which format specifier would you use to display a number as currency in C#?
A"F"
B"N"
C"D"
D"C"
✗ Incorrect
The "C" specifier formats numbers as currency, adding the currency symbol and decimals.
What does the "d" format specifier show when formatting a DateTime?
AShort date pattern (e.g., 6/5/2023)
BLong date pattern (e.g., Monday, June 5, 2023)
CTime only
DFull date and time
✗ Incorrect
"d" shows the short date pattern, which is a concise date format.
How would you format a number with thousands separators and two decimals in C#?
A"C"
B"F"
C"N"
D"P"
✗ Incorrect
"N" adds thousands separators and formats with two decimals by default.
Which format specifier shows the full year and month in a DateTime string?
A"yyyy-MM"
B"d"
C"D"
D"F"
✗ Incorrect
Using "yyyy-MM" custom format shows the full year and two-digit month.
What does the "F3" format specifier do when formatting a number?
AFormats as currency with 3 decimals
BFormats as fixed-point with 3 decimals
CFormats as number with thousands separators
DFormats as percentage with 3 decimals
✗ Incorrect
"F3" formats a number as fixed-point with exactly 3 decimal places.
Explain how to format a number as currency and as a fixed-point number in C#.
Think about how money and precise decimals look.
You got /4 concepts.
Describe the difference between the "D" and "d" date format specifiers in C#.
One is detailed, the other is brief.
You got /4 concepts.
Practice
(1/5)
1. Which format specifier in C# is used to display a number as currency with the correct symbol and two decimal places? decimal price = 123.456m;
easy
A. "N"
B. "C"
C. "P"
D. "D"
Solution
Step 1: Understand currency format specifier
The "C" format specifier converts a number to a currency string with the currency symbol and two decimals by default.
Step 2: Compare other specifiers
"N" formats as number with commas, "P" as percentage, "D" as decimal integer. Only "C" fits currency.
Final Answer:
"C" -> Option B
Quick Check:
Currency format = "C" [OK]
Hint: Currency format specifier is always "C" [OK]
Common Mistakes:
Using "N" which formats number with commas but no currency symbol
Using "P" which formats as percentage
Using "D" which is for integers only
2. Which of the following is the correct syntax to format a DateTime variable date to show only the year as four digits in C#?
easy
A. date.ToString("yy")
B. date.ToString("Year")
C. date.ToString("YYYY")
D. date.ToString("yyyy")
Solution
Step 1: Identify correct year format specifier
In C#, "yyyy" outputs the full four-digit year, while "yy" outputs two digits.
Step 2: Check case sensitivity and keywords
"YYYY" is invalid (case sensitive), and "Year" is not a recognized format string.
Final Answer:
date.ToString("yyyy") -> Option D
Quick Check:
Year four-digit format = "yyyy" [OK]
Hint: Use lowercase "yyyy" for full year, uppercase is invalid [OK]
Common Mistakes:
Using uppercase "YYYY" which is invalid
Using "yy" which shows only two digits
Using non-format strings like "Year"
3. What is the output of the following C# code?
double value = 0.7567; string result = value.ToString("P1"); Console.WriteLine(result);
medium
A. 75.7%
B. 0.8%
C. 0.76%
D. 75.67%
Solution
Step 1: Understand "P1" format specifier
"P" formats the number as a percentage by multiplying by 100 and adding "%". "1" means one decimal place.
Step 2: Calculate output
0.7567 * 100 = 75.67, rounded to one decimal place is 75.7, so output is "75.7%".
Final Answer:
75.7% -> Option A
Quick Check:
Percentage with 1 decimal = 75.7% [OK]
Hint: Percentage multiplies by 100 and adds % [OK]
Common Mistakes:
Not multiplying by 100 before formatting
Rounding incorrectly to 0.8%
Confusing decimal places with digits
4. The following code produces unexpected output. What is the problem?
The format string uses "yyyyy" which causes the fifth 'y' to be treated as a literal 'y', producing unexpected output; year specifier should be 4 'y' characters for full year.
Step 2: Validate other parts
Dashes do not need escaping, "MM" is correct for month, and DateTime supports ToString formatting.
Final Answer:
Too many 'y' characters in the format string -> Option C
Quick Check:
Year format max 4 'y' characters [OK]
Hint: Year format uses max 4 'y' characters [OK]
Common Mistakes:
Thinking dashes need escaping
Believing ToString can't format DateTime
Confusing month format with day format
5. You have a list of decimal prices and want to display each price formatted as currency with two decimals and a comma as thousand separator. Which code snippet correctly formats and prints the list?