Challenge - 5 Problems
Format Specifier Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of numeric format specifier
What is the output of the following C# code snippet?
C Sharp (C#)
double value = 1234.5678; string result = value.ToString("N2"); Console.WriteLine(result);
Attempts:
2 left
💡 Hint
The "N2" format specifier formats the number with commas and 2 decimal places.
✗ Incorrect
The "N2" specifier formats the number with thousands separators and rounds to 2 decimal places, so 1234.5678 becomes "1,234.57".
❓ Predict Output
intermediate2:00remaining
DateTime custom format output
What is the output of this C# code snippet if the date is March 5, 2024, 9:07:03 AM?
C Sharp (C#)
DateTime dt = new DateTime(2024, 3, 5, 9, 7, 3); string formatted = dt.ToString("yyyy-MM-dd HH:mm:ss"); Console.WriteLine(formatted);
Attempts:
2 left
💡 Hint
The format string uses four-digit year, two-digit month and day, and 24-hour time with leading zeros.
✗ Incorrect
The format string "yyyy-MM-dd HH:mm:ss" produces a date like "2024-03-05 09:07:03" with zero-padded numbers and 24-hour clock.
🔧 Debug
advanced2:00remaining
Identify the error in numeric format string
What error will this C# code produce?
C Sharp (C#)
int number = 255; string hex = number.ToString("X4Z"); Console.WriteLine(hex);
Attempts:
2 left
💡 Hint
Check if the format string "X4Z" is valid for ToString.
✗ Incorrect
The format string "X4Z" is invalid because "Z" is not a recognized format specifier, causing a FormatException at runtime.
❓ Predict Output
advanced2:00remaining
Output of date format with day and month names
What is the output of this C# code snippet for the date July 20, 2024?
C Sharp (C#)
DateTime dt = new DateTime(2024, 7, 20); string output = dt.ToString("dddd, MMMM dd, yyyy"); Console.WriteLine(output);
Attempts:
2 left
💡 Hint
"dddd" gives full day name, "MMMM" full month name.
✗ Incorrect
The format string "dddd, MMMM dd, yyyy" outputs the full day name, full month name, day with leading zero, and four-digit year.
🧠 Conceptual
expert3:00remaining
Understanding composite format strings with numbers and dates
Given the code below, what is the exact output?
C Sharp (C#)
DateTime dt = new DateTime(2024, 12, 31, 23, 59, 59); int count = 7; string result = string.Format("Date: {0:MM/dd/yyyy}, Count: {1:D3}", dt, count); Console.WriteLine(result);
Attempts:
2 left
💡 Hint
"D3" formats an integer with at least 3 digits, padded with zeros.
✗ Incorrect
The date is formatted as MM/dd/yyyy (month/day/year) and the integer count is formatted as a 3-digit number with leading zeros, so 7 becomes "007".