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

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

Post by ETS User »

if you say that
for( int i = 0; false; i++) x = 3; is also a compile time error because x= 3 is unreachable.
,which is in fact true, why it has been marked as correct answer?

and if it is not correct, it means it is only 2 correct answers there, right?

thanks

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

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

Post by admin »

The option that is marked as correct is:
for( int i = 0; i< 0; i++) x = 3;
The code that you quoted from explanation is not the same and is presented as another example of unreachable statement.

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

Michailangelo

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

Post by Michailangelo »

But i<0 is also false. So x=3; is also an unreachable statement.

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

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

Post by admin »

Michailangelo wrote:But i<0 is also false.
Yes, but you know that at run time and not at compile time. Compiler doesn't know the value of i.

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

Guest

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

Post by Guest »

for( int i = 0; i< 0; i++) x = 3;

Netbeans with 7.3 Beta has the warning, "For loops must use braces" for this statement:

for (int i = 0; i< 0; i++) x = 3;

Also, x should be defined in the question or individual answers, otherwise the user may assume that the statements won't compile as x was not defined.

-- Robert

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

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

Post by admin »

Guest wrote:for( int i = 0; i< 0; i++) x = 3;

Netbeans with 7.3 Beta has the warning, "For loops must use braces" for this statement:
This is not a test of good coding practices :) As long as it is valid, it is fair game :)
for (int i = 0; i< 0; i++) x = 3;

Also, x should be defined in the question or individual answers, otherwise the user may assume that the statements won't compile as x was not defined.

-- Robert
Assumption that x was not defined will lead the user to the conclusion that none of the given options will compile, which is not true because there are three correct options. So the only logical assumption is that x is declared appropriately. This statement has now been added to avoid the confusion.

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

ksnortum
Posts: 30
Joined: Fri Dec 07, 2012 6:09 pm
Contact:

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

Post by ksnortum »

In the explanation it says,

Code: Select all

for( int i = 0; false; i++) x = 3
but the option reads

Code: Select all

for( int i = 0; i< 0; i++) x = 3;
This is confusing.

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

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

Post by admin »

ksnortum wrote:In the explanation it says,

Code: Select all

for( int i = 0; false; i++) x = 3
but the option reads

Code: Select all

for( int i = 0; i< 0; i++) x = 3;
This is confusing.
Hello,
The code given in the option is valid code that is why it is a correct option. The code that you've quoted is only a part of the explanation and it is there to illustrates an invalid way to use the loop.

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

Ambiorix
Posts: 25
Joined: Thu Jan 10, 2013 8:45 am
Contact:

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

Post by Ambiorix »

admin wrote:
Michailangelo wrote:But i<0 is also false.
Yes, but you know that at run time and not at compile time. Compiler doesn't know the value of i.

HTH,
Paul.
I'm not clear why the compiler doesn't know the value of i when it's given explicitly in the same statement:

for( int i = 0; i< 0; i++) x = 3

Can you explain this?

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

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

Post by admin »

This is how compilers work. The determination of the actual value of a variable happens only at runtime. That is why it is called a "variable". The compiler takes into account only those values which are constants. So if you use a final variable, then the compiler will consider its value otherwise, the compiler does not assume anything about the value and leaves it to the runtime.

Your doubt is valid though and it is possible to construct a compiler that checks this kind of situation as well. However, java language designers decided against that. The Java Language Specification describes something called "compile time constants" and that determines what the compiler considers and what it ignores. You may want to check it out.

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

icepeanuts
Posts: 53
Joined: Thu Nov 22, 2012 12:01 am
Contact:

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

Post by icepeanuts »

this question is tricky, but fair enough.

baptize
Posts: 32
Joined: Wed Mar 14, 2012 5:45 pm
Contact:

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

Post by baptize »

admin wrote:This is how compilers work. The determination of the actual value of a variable happens only at runtime. That is why it is called a "variable". The compiler takes into account only those values which are constants. So if you use a final variable, then the compiler will consider its value otherwise, the compiler does not assume anything about the value and leaves it to the runtime.

Your doubt is valid though and it is possible to construct a compiler that checks this kind of situation as well. However, java language designers decided against that. The Java Language Specification describes something called "compile time constants" and that determines what the compiler considers and what it ignores. You may want to check it out.

HTH,
Paul.
so many exception to rules which is hard to keep track of...
If Oracle focus on those exception to rules no one will pass the exam :lol:

thchuong
Posts: 8
Joined: Wed Sep 10, 2014 2:42 am
Contact:

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

Post by thchuong »

baptize wrote: so many exception to rules which is hard to keep track of...
If Oracle focus on those exception to rules no one will pass the exam :lol:
=> very true! I hate this!!! :evil:

Kevin_C
Posts: 14
Joined: Mon Nov 03, 2014 5:18 am
Contact:

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

Post by Kevin_C »

I understand that

Code: Select all

for(int i = 0; false; i++){ // unreachable code }
fails, and

Code: Select all

for(int i = 0; i < 0; i++){ ... }
doesn't fail, because i is calculated during runtime.

But, what would happen if i was final, like so:

Code: Select all

for(final int i = 0; i < 0; i++){ ... }
Would i still be calculated during runtime, or are finals always calculated during compile time and this will also result in an unreachable code error?

EDIT: Ok, it is indeed like I said above, final is calculated during runtime, so it also gives an unreachable code error. So to change my question: Are all final statements calculated during compile time? Or are there exceptions to this where a final is calculated during runtime?

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

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

Post by admin »

Yes, final values are always considered by the compiler at compile time for determining unreachable code except in case of an if statement. The compiler specifically makes an exception for this:

final static boolean DEBUG = false;
if(DEBUG){
//ideally, this block is unreachable but the compiler allows it.
}

The reason is explained here: http://docs.oracle.com/javase/specs/jls ... #jls-14.21 (at the end)

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

coder007
Posts: 25
Joined: Wed Dec 17, 2014 9:29 pm
Contact:

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

Post by coder007 »

Kevin_C wrote:
But, what would happen if i was final, like so:

Code: Select all

for(final int i = 0; i < 0; i++){ ... }
I think this code is not valid?
"final" variable cannot be changed once assigned, but in for statement variable i supposed to be incremented...

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

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

Post by admin »

It certainly isn't valid:)
If you like our products and services, please help us by posting your review here.

coder007
Posts: 25
Joined: Wed Dec 17, 2014 9:29 pm
Contact:

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

Post by coder007 »

To complete this subject, in enhanced FOR statement we can use final key word:

Code: Select all

for (final String s : stringArr) .... 
Easy to confuse these things (at least for me :) )

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

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

Post by admin »

Good point:)
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 50 guests