0
0
C Sharp (C#)programming~20 mins

Parsing input to numeric types in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Parsing Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
  }
}
A
123
FormatException caught
B
12
123
C
FormatException caught
123
D
FormatException caught
12
Attempts:
2 left
💡 Hint
int.Parse expects a string that represents a whole number without decimals.
Predict Output
intermediate
2: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");
    }
  }
}
A3.14
BFailed to parse
C3,14
D0
Attempts:
2 left
💡 Hint
TryParse returns true if parsing succeeds and outputs the parsed value.
🔧 Debug
advanced
2: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?
ASystem.OverflowException
BSystem.FormatException
CSystem.ArgumentNullException
DNo error, number = 4294967173
Attempts:
2 left
💡 Hint
uint cannot represent negative numbers.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in parsing code
Which option contains a syntax error in parsing a string to int?
Aint.TryParse("100", out int result)
Bint.Parse("100";
Cint.TryParse("100", out result)
Dint.Parse("100")
Attempts:
2 left
💡 Hint
Check for missing or extra characters in method calls.
🚀 Application
expert
3: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"); } } }
A
Failed input1
Failed input2
B
Failed input1
1234.56
C
1234,56
1234.56
D
1234.56
Failed input2
Attempts:
2 left
💡 Hint
The French culture uses comma as decimal separator, not dot.