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

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

Moderator: admin

Post Reply
The_Nick

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

Post by The_Nick »

Hi,
I thoroughly understand why it prints out 2, therefore the whole logic thing of the difference between short circuit and not. However I would like to know why "method1(i++) " actually increments i.
Aren't primitive passed by value? passed a copy of the value? Why does the increment apply to the original copy?
Thanks a lot in advance.

The_Nick

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

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

Post by admin »

Because increment is not being done in method1. It is done in the place where the method is called.
If you like our products and services, please help us by posting your review here.

danspaes
Posts: 1
Joined: Sun Dec 15, 2013 4:08 pm
Contact:

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

Post by danspaes »

Hello all,

I didn't understood why does the 4th method wasn't called if bool1 is declared "true". The method aren't supposed to be called in case that bool1 is declared "false", as the comparison was done with a short term operator isn't that right?

Many thanks !

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

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

Post by admin »

danspaes wrote:Hello all,

I didn't understood why does the 4th method wasn't called if bool1 is declared "true". The method aren't supposed to be called in case that bool1 is declared "false", as the comparison was done with a short term operator isn't that right?

Many thanks !
No, since bool1 is true, there is no need to compute the right hand side operand of || because the whole expression will be true irrespective of what the right hand side operand returns. You might want to go through this to understand this: http://stackoverflow.com/questions/8759 ... circuiting

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

berensn
Posts: 4
Joined: Sat Jun 13, 2015 3:08 pm
Contact:

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

Post by berensn »

in line 2 it says: bool = ( bool2 && method1(i++) ); //2 where bool2= false, therefore it can't short circuit and should increment i making the answer 3 instead of 2.

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

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

Post by admin »

berensn wrote:in line 2 it says: bool = ( bool2 && method1(i++) ); //2 where bool2= false, therefore it can't short circuit and should increment i making the answer 3 instead of 2.
Since bool2 is false, && will short circuit the expression. Why do you think it will not?
-Paul.
If you like our products and services, please help us by posting your review here.

tonyincognito
Posts: 1
Joined: Mon Feb 20, 2023 4:02 am
Contact:

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

Post by tonyincognito »

berensn wrote:
Sun Jun 14, 2015 4:44 pm
in line 2 it says: bool = ( bool2 && method1(i++) ); //2 where bool2= false, therefore it can't short circuit and should increment i making the answer 3 instead of 2.
It's because && requires both operands to be true.

Because bool2 is false, it no longer checks the second operand because the statement will never be true (because bool2 was already false) and therefore it will not execute i++.

Hope that explains it better.

Post Reply

Who is online

Users browsing this forum: No registered users and 43 guests