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

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

Post Reply

Who is online

Users browsing this forum: witek_m and 8 guests