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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

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

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

admin
Site Admin
Posts: 8853
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

liviu_varlam
Posts: 3
Joined: Fri Dec 11, 2015 6:06 am
Contact:

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

Post by liviu_varlam »

since print "END" is unreachable code, how come it compiles?

admin
Site Admin
Posts: 8853
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Why do you think it is unreachable?
If you like our products and services, please help us by posting your review here.

olograph
Posts: 6
Joined: Mon Feb 01, 2016 3:13 pm
Contact:

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

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

Javier
Posts: 49
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

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

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

admin
Site Admin
Posts: 8853
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

radistmorse
Posts: 1
Joined: Sun Jan 21, 2018 8:02 am
Contact:

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

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

flex567
Posts: 201
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post 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");
   }
   }
}

admin
Site Admin
Posts: 8853
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

why ?
If you like our products and services, please help us by posting your review here.

flex567
Posts: 201
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post 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");
   }
   }
}

admin
Site Admin
Posts: 8853
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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?
If you like our products and services, please help us by posting your review here.

admin
Site Admin
Posts: 8853
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests