Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1046 :

Posted: Sun Dec 02, 2012 2:22 am
by ETS User
in this test only 2 correct answers, as it writen in the explanation, but it masked as wrong

Code: Select all

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.

Re: About Question enthuware.ocajp.i.v7.2.1046 :

Posted: Sun Dec 02, 2012 7:57 am
by admin
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.

Re: About Question enthuware.ocajp.i.v7.2.1046 :

Posted: Tue Jan 12, 2016 1:54 pm
by liviu_varlam
since print "END" is unreachable code, how come it compiles?

Re: About Question enthuware.ocajp.i.v7.2.1046 :

Posted: Tue Jan 12, 2016 6:16 pm
by admin
Why do you think it is unreachable?

Re: About Question enthuware.ocajp.i.v7.2.1046 :

Posted: Mon Feb 15, 2016 9:17 pm
by olograph
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.

Re: About Question enthuware.ocajp.i.v7.2.1046 :

Posted: Sun Jul 23, 2017 2:28 pm
by Javier
Hi Admin!

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 :(

Re: About Question enthuware.ocajp.i.v7.2.1046 :

Posted: Sun Jul 23, 2017 10:09 pm
by admin
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.

Re: About Question enthuware.ocajp.i.v7.2.1046 :

Posted: Sun Jan 21, 2018 8:11 am
by radistmorse
Javier wrote:Hi Admin!

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.

Re: About Question enthuware.ocajp.i.v7.2.1046 :

Posted: Fri Dec 14, 2018 7:10 pm
by flex567

Code: Select all

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 :

Code: Select all

public class TestClass{
   public static void main(String args[]){
      try{
         throw new IndexOutOfBoundsException( "Big Bang " );
      }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");
   }
   }
}

Re: About Question enthuware.ocajp.i.v7.2.1046 :

Posted: Fri Dec 14, 2018 8:38 pm
by admin
why ?

Re: About Question enthuware.ocajp.i.v7.2.1046 :

Posted: Sat Dec 15, 2018 5:49 am
by flex567
becuase of your answer:
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 .

Code: Select all

public class TestClass{
   public static void main(String args[]){
      try{
         throw new IndexOutOfBoundsException( "Big Bang " );
      }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");
   }
   }
}

Re: About Question enthuware.ocajp.i.v7.2.1046 :

Posted: Sat Dec 15, 2018 8:32 am
by admin
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?

Re: About Question enthuware.ocajp.i.v7.2.1046 :

Posted: Sat Dec 15, 2018 8:39 am
by admin
Please go through section 14.21 of JLS to learn all details on unreachable statements : https://docs.oracle.com/javase/specs/jl ... #jls-14.21