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

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

Moderator: admin

Post Reply
danielfb
Posts: 1
Joined: Tue Dec 01, 2015 5:07 pm
Contact:

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

Post by danielfb »

In

Code: Select all

boolean b1 = false;
boolean b2  = false; 
if (b2 = b1 != b2)
Why is, according to your answer, "b1 != b2" evaluated before the "b2 = b1"?
Is this because of the != having a higher precedence?

Thanks,
Dani.

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

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

Post by admin »

Yes, assignment operator has the least precedence.
If you like our products and services, please help us by posting your review here.

Deleted User 3513

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

Post by Deleted User 3513 »

Just wanted to clarify again, in the line if(b2=b1!=b2), first it evaluates b1!=b2 which returns false and then it is assigned to b2. Here b2 is evaluated in the if-condition which has the value of false and that results to the execution of the else block?

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

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

Post by admin »

That is correct.
If you like our products and services, please help us by posting your review here.

Bilbo2001
Posts: 1
Joined: Wed Jan 31, 2024 3:13 am
Contact:

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

Post by Bilbo2001 »

It turns out that the value is "true".

Two boolean variables b1 and b2 are declared and initialized with the value false.
Then, the condition b2 = b1 = !b2 is executed.
First, !b2 negates the value of b2, turning it into true since b2 is initially false.
This true value is then assigned to b1, making b1 also true.
Finally, this new true value is assigned to b2, making b2 true as well.
As a result, the condition becomes true because both variables b1 and b2 are now true.
"true" is printed to the console.

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

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

Post by admin »

The expression given in the question does not have b1 = !b2. It has b1 != b2. Please read the code carefully.
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 55 guests