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

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

Post by ETS User »

Heya,

Question:
What will be the result of attempting to compile and run the following program?

Code: Select all

class TestClass
{
   public static void main(String args[])
   {
      boolean b = false;
      int i = 1;
      do
      {
         i++ ;
      } while (b = !b);
      System.out.println( i );
   }
}
Since in this question the while(b=!b) is determining the loop, shouldn't the correct answer be infinite loop? There is no clause that terminates the loop?

Cheers,

Hugo

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

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

Post by admin »

No, you need to look at the value of the expression (b = !b) as a whole. To begin with, b is true. So !b is false. Therefore, false is being assigned back to b. Therefore, the value of the expression b = !b as a whole is false. So the loop will not be executed again.

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

Post by Guest »

Hello Paul,

Actually to begin with the value of boolean variable b is false...

I ran this in Eclipse's Debug feature and found that it changes to true then back to false and stops when it returns to false. So it goes.. 1) runs initial do with b = false 2) runs again with b = true 3) runs again but b = false so it stops.

This is a crazy boolean expression. :lol: I think I get it now.

Thanks all. :mrgreen:

Deepa

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

Post by Deepa »

Thanks for the explaination i got it!!

to be more clear the b in the while loop changes from true to false in the second time thats why the do loop ends and prints 3.

Ryhor

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

Post by Ryhor »

Actually there is a simpler explanation.

We need to count how many times do-while loop runs. First check: b is assigned to true (b = !initial value of b(=false)), that's why loop goes again, but second check stops the loop: b is assign to false (b = !previous value of b(=true)).

Loop's body goes two times, that's why correct answer is 1+2 = 3.

Brian B
Posts: 13
Joined: Mon Jun 16, 2014 1:07 pm
Contact:

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

Post by Brian B »

This one stumped me for a time and then it finally occurred to me how it worked; at least to my own understanding.

The key to the entire question is hinged on the assignment operator (=) in the while loop.

Variable b's value is set to false entering the loop and is then assigned a value of 'true' when it enters the while loop. i's value is incremented to 2 and the it enters the loop another time.

i's value is incremented to 3.

This time b's value is assigned false when the expression is executed and the while condition evaluates to false and the loop exits.

i's value of 3 is printed to the console.

Would that be correct?

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

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

Post by admin »

Yes, that is correct. Only thing is the way you've written " b's value is set to false" and "b's value is assigned false" is not proper. You should write, "b is set to false" or "b is assigned false".

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

subhamsdalmia
Posts: 32
Joined: Sat May 02, 2015 11:57 pm
Contact:

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

Post by subhamsdalmia »

Code: Select all

class P3{
	   public static void main(String args[]){
	      boolean b = false;
	      System.out.println("before do loop "+b);
	      int i = 1;
	      do{
	         i++ ;
	         System.out.println("inside loop "+b);
	         System.out.println("i "+i);
	      } while (b = !b );System.out.println("ouside loop "+b);
	      System.out.println( i );
	   }
	}
Made changes to understand,
Output:
before do loop false
inside loop false
i 2
inside loop true
i 3
ouside loop false
3

Don't understand why inside do loop it is true and outside it is false.
Does while has anything to do with it?

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

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

Post by admin »

Because the value of b is being changed in the while condition expression b = !b.
If you like our products and services, please help us by posting your review here.

vlezz94
Posts: 12
Joined: Wed Sep 28, 2016 6:31 am
Contact:

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

Post by vlezz94 »

Hi Paul!

I got this one correct, but the thing is that I actually don't understand why the do-while loop loops a second time. Because as I see the code:

Code: Select all

class TestClass{
   public static void main(String args[]){
      boolean b = false;
      int i = 1;
      do{
         i++ ;
      } while (b = !b);
      System.out.println( i );
   }
}

At first sight what I get is this:

Code: Select all

class TestClass{
   public static void main(String args[]){
      boolean b = false;
      int i = 1;
      do{
         i++ ;
      } while (false = !false); // This is the line of code that breaks my head
      System.out.println( i );
   }
}

vlezz94
Posts: 12
Joined: Wed Sep 28, 2016 6:31 am
Contact:

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

Post by vlezz94 »

Got it!

Don't know why I saw while(false = !false) instead of just while(true)

teodor
Posts: 1
Joined: Wed Dec 14, 2016 4:59 pm
Contact:

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

Post by teodor »

If I understand well, the code of this question is similar to this:

Code: Select all

class TestClass{
   public static void main(String args[]){
      boolean b = false;
      int i = 1;
      do{
         i++ ;
         b=!b;
      } while (b);
      System.out.println( i );
   }
}
is it correct?

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

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

Post by admin »

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

MariusBV
Posts: 1
Joined: Sat Jun 03, 2017 8:43 am
Contact:

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

Post by MariusBV »

Hello,

simpler way to see this question is to read what Oracle says about while loops:
"The while and do-while Statements

The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as:

while (expression) {
statement(s)
}

The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block."

Due to previous a do while loop woks as long as condition is evaluated to true and when it's false will stop.

In our case the boolean value of b is first set to false. Afterward the program it's entering the loop and increments the integer value. At the end of first loop b has true value due to b=!b. At the second loop the value of i is incremented to 2 and at the end of first loop b is false because of b=!b. Finally it exists due to false value of do-while condition.

Post Reply

Who is online

Users browsing this forum: No registered users and 243 guests