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

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.964 :

Post by ETS User »

System.out.println("c") is always unreachable , so why not it cause compiler error.
It the answer is: The compiler cannot decide during compile time that the statement is unreachable, then how do we decide if the compiler will be able to decide or not during compile time?

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

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

Post by admin »

The detailed rules are given here: Section 14.21 of JLS

It may seem complicated but the general idea is to look for compile time constants i.e. final variables and literals. For example,

Code: Select all

int x = 5;
while (x > 100){ System.out.println(x); }
will work. But this will not:

Code: Select all

final int x = 5;
while (x > 100){ System.out.println(x); }
because compiler knows that x cannot change from 5.

On top of this, there are some exceptions such as the if statement. It is explained in the above mentioned section.

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

jeremy_dd
Posts: 9
Joined: Mon Nov 11, 2013 3:11 pm
Contact:

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

Post by jeremy_dd »

I pasted the code in my eclipse, it fails to compile because m1 is supposed to throw an exception, which is not caught...

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

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

Post by admin »

jeremy_dd wrote:I pasted the code in my eclipse, it fails to compile because m1 is supposed to throw an exception, which is not caught...
The question and the explanation are correct. Please make sure you paste the code exactly as given.

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

KKreator
Posts: 2
Joined: Tue May 17, 2016 2:33 am
Contact:

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

Post by KKreator »

jeremy_dd wrote:I pasted the code in my eclipse, it fails to compile because m1 is supposed to throw an exception, which is not caught...
I think you forgot to add "...throws Exception" to the main function.

JavaSoftware
Posts: 6
Joined: Sat Jun 03, 2017 1:43 pm
Contact:

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

Post by JavaSoftware »

Hi! But if we catch the exception in catch block and print some message say "message" , then the statement System.out.println("C"); will be executed ?

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

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

Post by admin »

What happened when you tried it out?
Paul.
If you like our products and services, please help us by posting your review here.

JavaSoftware
Posts: 6
Joined: Sat Jun 03, 2017 1:43 pm
Contact:

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

Post by JavaSoftware »

Thank you for your promotion to write and test code by myself and sorry for my hastiness.
And the statement System.out.println("C"); will be printed after we catch the exception :)

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

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

Post by admin »

Very good :)
This exam is all about writing short code examples to understand the basic concepts.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 44 guests