Challenge - 5 Problems
Parsing Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of parsing a valid integer string?
Consider the following Java code snippet that parses a string to an integer. What will be printed?
Java
public class Main { public static void main(String[] args) { String input = "123"; int number = Integer.parseInt(input); System.out.println(number); } }
Attempts:
2 left
💡 Hint
Think about what Integer.parseInt does with a valid numeric string.
✗ Incorrect
The method Integer.parseInt converts the string "123" to the integer 123, which is then printed.
❓ Predict Output
intermediateWhat happens when parsing a non-numeric string?
What will be the output or result of running this Java code?
Java
public class Main { public static void main(String[] args) { String input = "abc"; int number = Integer.parseInt(input); System.out.println(number); } }
Attempts:
2 left
💡 Hint
What does Integer.parseInt do if the string is not a valid number?
✗ Incorrect
Parsing a non-numeric string with Integer.parseInt throws a NumberFormatException.
❓ Predict Output
advancedParsing a floating-point string with Integer.parseInt
What will happen when this code runs?
Java
public class Main { public static void main(String[] args) { String input = "12.34"; int number = Integer.parseInt(input); System.out.println(number); } }
Attempts:
2 left
💡 Hint
Integer.parseInt expects a string representing an integer only.
✗ Incorrect
The string "12.34" is not a valid integer format, so Integer.parseInt throws NumberFormatException.
❓ Predict Output
advancedParsing a floating-point string with Double.parseDouble
What will be the output of this Java code?
Java
public class Main { public static void main(String[] args) { String input = "12.34"; double number = Double.parseDouble(input); System.out.println(number); } }
Attempts:
2 left
💡 Hint
Double.parseDouble can parse decimal numbers from strings.
✗ Incorrect
Double.parseDouble converts the string "12.34" to the double value 12.34, which is printed.
🧠 Conceptual
expertWhat is the value of 'result' after parsing with radix 16?
Given the code below, what is the value of the variable 'result' after execution?
Java
public class Main { public static void main(String[] args) { String hex = "1A"; int result = Integer.parseInt(hex, 16); System.out.println(result); } }
Attempts:
2 left
💡 Hint
Hexadecimal 1A equals decimal 26.
✗ Incorrect
Parsing "1A" as a base-16 number converts it to decimal 26.
