What type of exception is thrown by parseInt() if it gets illegal data?

  1. ArithmeticException

  2. RunTimeException

  3. NumberFormatException

  4. NumberError


Correct Option: C
Explanation:

To solve this question, the user needs to be familiar with the parseInt() method and the types of exceptions it can throw.

The parseInt() method is used to convert a string to an integer. If the string contains illegal data (i.e. not a valid integer), the method will throw an exception.

Now, let's go through each option and explain why it is right or wrong:

A. ArithmeticException: This exception is thrown when an arithmetic operation produces an error, such as dividing by zero. It is not related to the parseInt() method, so this option is incorrect.

B. RunTimeException: This is a general exception that can be thrown at runtime for a variety of reasons. It is not specific to the parseInt() method, so this option is incorrect.

C. NumberFormatException: This is the correct answer. This exception is thrown by parseInt() when the string being parsed contains illegal data. For example, if the string "abc" is passed to parseInt(), a NumberFormatException will be thrown because "abc" is not a valid integer.

D. NumberError: This is not a standard Java exception, so this option is incorrect.

Therefore, the answer is: C. NumberFormatException

Find more quizzes: