Challenge - 5 Problems
Parsing Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of parsing valid and invalid numeric strings
What is the output of the following C# code?
C Sharp (C#)
using System; class Program { static void Main() { string input1 = "123"; string input2 = "12.3"; int result1 = int.Parse(input1); try { int result2 = int.Parse(input2); Console.WriteLine(result2); } catch (FormatException) { Console.WriteLine("FormatException caught"); } Console.WriteLine(result1); } }
Attempts:
2 left
💡 Hint
int.Parse expects a string that represents a whole number without decimals.
✗ Incorrect
The string "123" parses correctly to int 123. The string "12.3" is not a valid integer format, so int.Parse throws a FormatException, which is caught and prints the message. Then the first parsed integer 123 is printed.
❓ Predict Output
intermediate2:00remaining
Parsing float with TryParse and output
What will be the output of this C# code?
C Sharp (C#)
using System; class Program { static void Main() { string input = "3.14"; if (float.TryParse(input, out float value)) { Console.WriteLine(value); } else { Console.WriteLine("Failed to parse"); } } }
Attempts:
2 left
💡 Hint
TryParse returns true if parsing succeeds and outputs the parsed value.
✗ Incorrect
The string "3.14" is a valid float representation in C# when using the invariant culture or default culture with dot decimal separator, so TryParse returns true and prints 3.14.
🔧 Debug
advanced2:00remaining
Why does parsing "-123" to uint fail?
Consider this code snippet:
uint number = uint.Parse("-123");
What error will this code produce when run?
Attempts:
2 left
💡 Hint
uint cannot represent negative numbers.
✗ Incorrect
Parsing "-123" to uint causes an OverflowException because uint only accepts values from 0 to 4,294,967,295. Negative values are out of range.
📝 Syntax
advanced2:00remaining
Identify the syntax error in parsing code
Which option contains a syntax error in parsing a string to int?
Attempts:
2 left
💡 Hint
Check for missing or extra characters in method calls.
✗ Incorrect
Option B is missing a closing parenthesis and semicolon is misplaced, causing a syntax error.
🚀 Application
expert3:00remaining
Result of parsing multiple inputs with culture-specific decimal separator
Given this code snippet:
using System;
using System.Globalization;
class Program {
static void Main() {
string input1 = "1234,56";
string input2 = "1234.56";
var culture = new CultureInfo("fr-FR");
if (double.TryParse(input1, NumberStyles.Any, culture, out double val1)) {
Console.WriteLine(val1);
} else {
Console.WriteLine("Failed input1");
}
if (double.TryParse(input2, NumberStyles.Any, culture, out double val2)) {
Console.WriteLine(val2);
} else {
Console.WriteLine("Failed input2");
}
}
}
Attempts:
2 left
💡 Hint
The French culture uses comma as decimal separator, not dot.
✗ Incorrect
In "fr-FR" culture, "1234,56" parses correctly as 1234.56 double value. "1234.56" fails because dot is not a valid decimal separator in French culture.