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

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

Post by ETS User »

Good day. In this question also may create for loop.
for(;!isDone();){}

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

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

Post by admin »

That's a good point. Seems like a hack but works.
If you like our products and services, please help us by posting your review here.

Guest

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

Post by Guest »

In the code from the question, the contents of the loop will always be run at least once before checking "isDone()" at the end. If we replace the original loop with "for(;!isDone();){}" we're not guaranteed to run the loop at least once, because the question says nothing about what "isDone()" actually does. For all we know it might return true even before reaching the loop. So not only does it look sort of look like a hack, but it might not give the same result either.

Sweetpin2
Posts: 27
Joined: Thu Feb 07, 2013 9:46 pm
Contact:

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

Post by Sweetpin2 »

This question seems to have two answers as both of below code will work & is more readable. I don't know why do..while is only correct here.


for(;!isDone();) {//additional valid code}

do{//additional valid code}while( !isDone() );
Last edited by Sweetpin2 on Thu Mar 07, 2013 4:11 pm, edited 1 time in total.

Sweetpin2
Posts: 27
Joined: Thu Feb 07, 2013 9:46 pm
Contact:

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

Post by Sweetpin2 »

Below is a sample code to justify my answer

public class For_Test {


static int i = 0;

public static void main(String[] args) {

for(;!isDone();)
{
System.out.println("Hello");
}

do
{
System.out.println("Hello World");

}while(!isDone());


}

static boolean isDone()
{
i++;
if(i==1) return false;
else return true;
}

}

Guest

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

Post by Guest »

Now I can't remember the question exactly, but the question doesn't say anything about isDone() does it? This means that when you use a for-loop, you're not guaranteed to the run code within the brackets, while using the do-while-loop, you're guaranteed to run the code at least once?

Jokumo♫
Posts: 6
Joined: Wed Apr 20, 2016 9:56 am
Contact:

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

Post by Jokumo♫ »

The explanation for the for-loop is wrong. In case you choose to use a for-loop instead of a do-while-loop you would have to code it like this:

Code: Select all

for(;true;){
    //additional valid code
    if(isDone()) break;
}
...a for-loop without initialization block and update statement acts just the same as a while-loop.

The question now is: Is a while-loop the same as a do-while-loop? :twisted:

zhengye1
Posts: 17
Joined: Wed Jan 07, 2015 12:06 am
Contact:

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

Post by zhengye1 »

Jokumo♫ wrote:The explanation for the for-loop is wrong. In case you choose to use a for-loop instead of a do-while-loop you would have to code it like this:

Code: Select all

for(;true;){
    //additional valid code
    if(isDone()) break;
}
...a for-loop without initialization block and update statement acts just the same as a while-loop.

The question now is: Is a while-loop the same as a do-while-loop? :twisted:
The difference for while-loop and do-while-loop is:
For while-loop, it will check the condition first, if condition matches, then execute the code that inside the while block.

For do-while-loop, it will always run at least once, then check the condition to check does the block of code need to run again or not

edcrader
Posts: 1
Joined: Sun Oct 30, 2016 10:59 am
Contact:

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

Post by edcrader »

zhengye1 wrote:
Jokumo♫ wrote:The explanation for the for-loop is wrong. In case you choose to use a for-loop instead of a do-while-loop you would have to code it like this:

Code: Select all

for(;true;){
    //additional valid code
    if(isDone()) break;
}
...a for-loop without initialization block and update statement acts just the same as a while-loop.

The question now is: Is a while-loop the same as a do-while-loop? :twisted:
The difference for while-loop and do-while-loop is:
For while-loop, it will check the condition first, if condition matches, then execute the code that inside the while block.

For do-while-loop, it will always run at least once, then check the condition to check does the block of code need to run again or not
Your absolutely right on the way the do-while and while loop work. However, in this case you have to pay close attention to the while(true). The condition is true and will always be true (causing an infinite loop). This means there is no chance the while loop won't run at least once.

Therefore, in this case the program functionality is similar even though functionality of the loops are different.

dvc1190
Posts: 15
Joined: Tue Feb 25, 2014 3:14 am
Contact:

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

Post by dvc1190 »

Sweetpin2 wrote:Below is a sample code to justify my answer

public class For_Test {


static int i = 0;

public static void main(String[] args) {

for(;!isDone();)
{
System.out.println("Hello");
}

do
{
System.out.println("Hello World");

}while(!isDone());


}

static boolean isDone()
{
i++;
if(i==1) return false;
else return true;
}

}
Your code example is written so that isDone will return true at least once. What if isDone always returned false? Or what if your first value was 1? Then the for loop would not behave the same as a do-while nor will it behave the same as the bad example we are trying to correct in the question.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post by OCAJO1 »

Just wondering what type of a code would have made "use a for loop" the correct answer?

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

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

Post by admin »

for is perfectly suitable when you need to have an iteration variable and you want to use it inside the loop. Something like this:

Code: Select all

for(int i=0; i<10; i++){
  array[i] = i*i; //using iteration variable to initialize all array elements 
}
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post by OCAJO1 »

Actually I was more referring to the fact that the following explanation was given, but the answer itself was NOT chosen as the right one.

"The following is how it can be done using a for loop: for(;!isDone();) { //additional valid code }"

I wanted to find out what kind of code in the question, would have made the above answer, rather than the do-while, the correct answer?

Thanks

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

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

Post by admin »

If the objective is to improve readability, this code wouldn't ever be the correct answer even though it is a valid piece of code.
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 93 guests