Bird
0
0

Why does Integer.parseInt("08", 8) throw a NumberFormatException in Java?

hard📝 Conceptual Q10 of 15
Java - Command Line Arguments
Why does Integer.parseInt("08", 8) throw a NumberFormatException in Java?
ABecause leading zeros are not allowed in parseInt
BBecause parseInt cannot parse strings with more than one digit
CBecause parseInt only accepts positive numbers without zeros
DBecause "08" is interpreted as octal and 8 is invalid digit in octal
Step-by-Step Solution
Solution:
  1. Step 1: Understand parseInt with radix and leading zeros

    When using parseInt with radix 8 (octal), digits must be 0-7; "08" is invalid octal.
  2. Step 2: Check default parseInt behavior

    parseInt("08") with default radix 10 works fine; error occurs if radix 8 is specified.
  3. Final Answer:

    Because "08" is interpreted as octal and 8 is invalid digit in octal -> Option D
  4. Quick Check:

    Leading zero triggers octal radix, invalid digit causes exception [OK]
Quick Trick: Leading zero may cause octal parsing errors [OK]
Common Mistakes:
  • Assuming leading zeros always cause error
  • Ignoring radix parameter
  • Thinking parseInt rejects zeros

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes