Prashnikaप्रश्निका
Code
About
‹ Back to the paper

Question 15

1 mark

Computer ScienceISC Computer Science - Competency Focused Practice Questions (CISCE, August 2024)MCQObjects

Choose the correct option.

Which one of the given exceptions is raised when a user enters a String literal “hello” on the…

Which one of the given exceptions is raised when a user enters a String literal “hello” on the execution of following code:
import java.util.*; 
class Message 
{ 
    public void doIt( ) 
    { 
        System.out.println("Enter a message"); 
        int s = (new Scanner(System.in)).nextInt( ); 
        System.out.println(s); 
    } 
}
  • (a)InputMismatchException
  • (b)InputFormatException
  • (c)NullPointerException
  • (d)NumberFormatException

Answer

Answer

Correct option: a

Answer: (a) InputMismatchException. Scanner's nextInt( ) expects the next token to be a valid integer. When the user types the non-numeric literal "hello", nextInt( ) cannot parse it as an int and throws an InputMismatchException.