About Question com.enthuware.ets.scjp.v6.2.467 :

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 com.enthuware.ets.scjp.v6.2.467 :

Post by ETS User »

Which of the following code snippets will compile without any errors?

Answer 1: while (false) { x=3; }
Explanation
while (false) { x=3; } is a compile-time error because the statement x=3; is not reachable;
I get a compile time error that states "x cannot be resolved". I do not get any compile time message stating the code is unreachable.
when I add the following line of code before this:

Code: Select all

int x;
I get no compile time errors at all.

Guest

Re: About Question com.enthuware.ets.scjp.v6.2.467 :

Post by Guest »

Never mind.... I got that compiler error. There was another compiler error having to do with a duplicate definition of "x" in my code that was being flagged first by the compiler....

Guest

Re: About Question com.enthuware.ets.scjp.v6.2.467 :

Post by Guest »

The explanation says that "for( int i = 0; false; i++) x = 3; is also a compile time error because x= 3 is unreachable.", so, why the option is marked as correct (whith no compile errors).

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

Re: About Question com.enthuware.ets.scjp.v6.2.467 :

Post by admin »

Hello, The option that is marked as correct is for( int i = 0; i< 0; i++) x = 3;

The statement given in the explanation is talking about a similar but different situation.
If you like our products and services, please help us by posting your review here.

borkutip
Posts: 13
Joined: Sat Mar 08, 2014 1:20 am
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.467 :

Post by borkutip »

Hello,

I think, that the explanation is not very clear, especially the connection between
for( int i = 0; i< 0; i++) x = 3;
and
for( int i = 0; false; i++) x = 3;

is not obvious (for me).

I think, that the clue is that the compiler checks only compile-time constants:
for example:
This is OK:
boolean b = false;
for (int i = 0; b; i++) {x = 3;};
and this is unreachable code:
final boolean b = false;
for (int i = 0; b; i++) {x = 3;};


So, here:
for( int i = 0; i< 0; i++) x = 3;
because "i" is not a compile-time constant, the compiler will not check (or can not see?) unreachable code (however it is obvious for a human-eye to
detect unreachable code)

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

Re: About Question com.enthuware.ets.scjp.v6.2.467 :

Post by admin »

Enhanced the explanation.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

KevinGombe
Posts: 1
Joined: Wed Aug 09, 2023 11:31 am
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.467 :

Post by KevinGombe »

How is it that if (false) { f=3; } compiles fine but the statement f=3 is unreachable

but the second statement :
while(false){
f=4;
}
rightfully doesn't compile for the same reason

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

Re: About Question com.enthuware.ets.scjp.v6.2.467 :

Post by admin »

Technically, both contain unreachable statements and should not compile but the first one i.e. if(false) { } is made an exception. It is allowed explicitly by Java specification to allow conditional compilation, which is useful for writing log statements such as:

if(LOG_ENABLED) {
out.println("logging...");
}

Which book are you studying for the exam? This is explained in OCAJP Fundamentals by Hanumant Deshmukh and also in the explanation.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 225 guests