What will be the output of the following program:
public class TestClass{
public static void main(String args[]){
try{
m1();
}catch(IndexOutOfBoundsException e){
System.out.println("1");
throw new NullPointerException();
}catch(NullPointerException e){
System.out.println("2");
return;
}catch (Exception e) {
System.out.println("3");
}finally{
System.out.println("4");
}
System.out.println("END");
}
// IndexOutOfBoundsException is a subclass of RuntimeException.
static void m1(){
System.out.println("m1 Starts");
throw new IndexOutOfBoundsException( "Big Bang " );
}
}
Correct answers are
1) The program will print m1 Starts, 1 and 4, in that order.
and
2)END will not be printed.
but program say that "The program will print m1 Starts." correct.
The issue here is that the program does print "m1 Starts". It also prints more stuff but it does print "m1 starts".
While it is easy to change the option here, we have kept it like this to sensitize the users that they may get something like this in the exam where the option might not give the complete output but only partial output.
So just watch out for this.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
While it is easy to change the option here, we have kept it like this to sensitize the users that they may get something like this in the exam where the option might not give the complete output but only partial output.
So just watch out for this.
Might want to add this to the correction message. It really is confusion fuel.
The key point here is that the compiler doesn't know that the call to m1() will always result in an exception. We know that because we execute the code mentally, but the compiler doesn't execute any code. Thus, as far as the compiler is concerned, the last line may execute.
If you like our products and services, please help us by posting your review here.
I don´t understand how is possible that this code compiles if the program never is going to reach the snippet of code:
System.out.println("END");
So, is there the possibility to be reached that snippet of code in this program?
Thank you very much, I need help
I also want to point out, that this snippet is reachable, even as it is, since the method System.out.println can theoretically throw a RuntimeException depending on the JVM implementation, which will be successfully caught by the "catch (Exception e)" block, and the program will terminate properly.
The key point here is that the compiler doesn't know that the call to m1() will always result in an exception. We know that because we execute the code mentally, but the compiler doesn't execute any code. Thus, as far as the compiler is concerned, the last line may execute.
Then this should be compiler error with unreachable code :
The key point here is that the compiler doesn't know that the call to m1() will always result in an exception. We know that because we execute the code mentally, but the compiler doesn't execute any code. Thus, as far as the compiler is concerned, the last line may execute.
Here in the code I had modified it so it is clear that "END " cant be reached and so the compiler should complain about unreachable code. But it doesn't .
But why would compiler know that END wont ever be reached?
Compiler doesnt execute any code, so how will it know what happens in the try block at run time?
If you like our products and services, please help us by posting your review here.