What is the output of the following sequence?

Scanner scan = new Scanner( System.in );

int number = 0;

try

{

number = scan.nextInt( ); // User enters 12, hits ENTER

System.out.println( "1: " + number );

number = scan.nextInt( ); // User enters 56, hits ENTER

System.out.println( "2: " + number );

number = scan.nextInt( ); // User enters 99ABC, hits ENTER

System.out.println( "3: " + number );

}

catch( Exception e)

{

String s = scan.nextLine( );

System.out.println( "4: " + number );

}

finally

{

number = scan.nextInt( ); // User enters 100, hits ENTER

System.out.println( "5: " + number );

}

Respuesta :

Answer:

The output of the given program is

12

1:12

56

2:56

99ABC

4:56

100

5:100

Explanation:

In this given code if user enters 12 then it print 1:12 after that user enters 56 it prints 2:56, when enter non integer on third statement it will catch exception and prints the value which user entered previously i.e 56...so if user enter non integer on third statement it skip these two statement System.out.println( "3: " + number ); ,  String s = scan.nextLine( ); so statement 4 is executed  which is in catch statement and print "4:56" and finally it enters to finally block and  again user will prompt to enter number..if user enter 100  it print "5:100".