PRASHNIKAप्रश्निका
‹ Back to the paper

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

Computer Science20251 markMCQ
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); 
    } 
}
  • aInputMismatchException
  • bInputFormatException
  • cNullPointerException
  • dNumberFormatException

Answer

Answer

AI

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.
Objects

From ISC Computer Science - Competency Focused Practice Questions (CISCE, August 2024), question 15.