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

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

Moderator: admin

Post Reply
rafaparche
Posts: 5
Joined: Tue Apr 14, 2015 9:32 am
Contact:

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

Post by rafaparche »

Hello,

below code iterates only once so I thought that i would be 1 instead 0, why?

Code: Select all

for (i=0 ;       ; i++) break; 
thanks!

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

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

Post by admin »

Iteration doesn't complete because of the break so i++ is never executed.
If you like our products and services, please help us by posting your review here.

madhurisatish
Posts: 15
Joined: Thu Sep 10, 2015 3:50 pm
Contact:

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

Post by madhurisatish »

I want to know what does for ( ; i<5?false:true ; ); mean??

Basically, I have a question like what does for (; i<5;); mean?

Please explain

madhurisatish
Posts: 15
Joined: Thu Sep 10, 2015 3:50 pm
Contact:

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

Post by madhurisatish »

does it mean it doesn't have body. Is it same like

Code: Select all

for(;i<5;) {
             }

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

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

Post by admin »

madhurisatish wrote:does it mean it doesn't have body. Is it same like

Code: Select all

for(;i<5;) {
             }
Yes.
If you like our products and services, please help us by posting your review here.

JuergGogo
Posts: 28
Joined: Mon Sep 25, 2017 8:16 am
Contact:

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

Post by JuergGogo »

Code: Select all

class TestClass{
   public static void main(String args[]){
      int i = 0;
      for (i=1 ;  i<5  ; i++) continue;  //(1)
      for (i=0 ;       ; i++) ;                                  // break;       //(2)

      for (    ; i<5?false:true ;    );     //(3)   --> compiler error: unreachable statement
   }
}
I have chosen the second Loop as invalid, but it works fine. When eliminating the break statement, you get a compiler error.

tanushri
Posts: 5
Joined: Tue Dec 12, 2017 11:45 pm
Contact:

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

Post by tanushri »

should it compile if the local variable i is being redefined (in the for- loops) within the same scope, ie main method?

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

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

Post by admin »

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

tanushri
Posts: 5
Joined: Tue Dec 12, 2017 11:45 pm
Contact:

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

Post by tanushri »

i didnt run it. But was comparing with Question 8 of test 7.

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

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

Post by flex567 »

From the answer:

Code: Select all

A continue statement can occur in and only in a for, while or do-while loop
Is the for each loop included?
In which loop continue cant occur?

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

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

Post by admin »

Yes, it can appear in all loops including enhanced for. Explanation has been updated to make it clear.
thanks!
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

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

Post by crazymind »

Isn't it suppose be an unreachable code error? first loop keep iterates since continue then gets out. Other two inner loops never get executed.

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

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

Post by admin »

No, because the statements use variables that are not compile time constants. That is why the compiler cannot know the value of the loop variables at compile time.
If you like our products and services, please help us by posting your review here.

li_hanglin@bah.com
Posts: 6
Joined: Mon Jan 28, 2019 4:18 pm
Contact:

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

Post by li_hanglin@bah.com »

In the explanation, it says: for ( ; i<5?false:true ; ); never iterates because i is less than 5 (it is 0 because of //2).

isn't the i in //2 local to that for loop and go out of scope in //3 so for ( ; i<5?false:true ; ) would use the i from int i = 0?

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

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

Post by admin »

No, //2 doesn't contain declaration i.e. int i=0; it contains only assignment i.e. i=0;
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 39 guests