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

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

Moderator: admin

Post Reply
Dan Lee
Posts: 4
Joined: Tue May 20, 2014 12:41 am
Contact:

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

Post by Dan Lee »

I don't understand this,

boolean b1 = false;
int i1 = 2;
int i2 = 3;
if (b1 = i1 == i2)

My understanding,
i1 == i2 is false
b1 = i1 == i2 which is "if (false= false)" should be false

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

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

Post by admin »

Why do you think false = false should compile?

Dan Lee
Posts: 4
Joined: Tue May 20, 2014 12:41 am
Contact:

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

Post by Dan Lee »

Hm... I think should be,

if (b1 = false) which is false. Am I correct?

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

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

Post by admin »

= is assignment operator and false is not a variable. Why do you think you can assign false to false?
"if (b1 = false)" is valid but that is not same as false = false.

if(b1 = false) implies you are assigning false to b1 and so the expression resolves to if(b1) i.e. if(false)

Hence, the complete expression b1 = i1 == i2 resolves like this:
b1 = (2 == 3)
=> b1 = false
=>b1
=>false

HTH,
Paul.

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

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

Post by gparLondon »

Here I would like to add a point. This question is based on operator precedence, == has higher precedence than =. Hence, 2==3 results into false, which is assigned to boolean variable, b1. so, if(false){}else{} here, else part is evaluated.

Venceslas
Posts: 15
Joined: Thu Feb 05, 2015 3:50 pm
Contact:

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

Post by Venceslas »

I agree, I have failed to this question due to operator precedence. I have the feeling that the explanation should explain that JVM interpret the expression like if (b1 = (i1 == i2) )

By the way, is it required from us to know the precedence between the full range of the operators please?

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

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

Post by admin »

It has now been added to the explanation.


Yes, you need to know operator precedence.

NickWoodward
Posts: 29
Joined: Mon Mar 30, 2015 6:00 pm
Contact:

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

Post by NickWoodward »

thought i'd quickly log in and just echo what the above posters have said.

explaining this question from the perspective of precendence makes it a simple answer, and to date it appears not to have been added (to my version at least!).

although i understand the answer, i don't think the current explanation is very good (sorry!).

fantastic piece of software though, it's a great help!

nick

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

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

Post by admin »

I see that the explanation is included in question bank version 2.20.
The expression b1 = i1 == i2 will be evaluated as b1 = (i1 == i2) because == has higher precedence than =. Further, all an if statement needs is a boolean. Now i1 == i2 returns false which is a boolean and since b1 = false is an expression and every expression has a return value (which is actually the Left Hand Side of the expression), it returns false which is again a boolean. Therefore, in this case, the else condition will be executed.

TimothyTuckers
Posts: 2
Joined: Fri Jan 08, 2016 3:06 pm
Contact:

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

Post by TimothyTuckers »

so, basically the first condition is:

if (false) {do this}. since the first condition is false, the program moves to the next step.

in this case the next step is else {do this}

is that a correct way of thinking about it??

thanks in advance.

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

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

Post by admin »

Yes, that is correct.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests