About Question enthuware.ocajp.i.v8.2.948 :

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

Moderator: admin

Post Reply
admin
Site Admin
Posts: 10045
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.948 :

Post by admin »

Can you be bit more clear about your querry? As the explanation says, the print stataement is in the call to method(1), so that output is coming from there irrespective of how the boolean expression is evaluated.

so yes, you should assign to "bool" the result from the parenthesis later but how will that change the output which is already printed?
If you like our products and services, please help us by posting your review here.

juliusacar
Posts: 2
Joined: Fri Dec 21, 2018 4:01 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.948 :

Post by juliusacar »

For this part:

bool = (bool1 || method1("4")); //4

bool1 = true;

public static boolean method1(String str){
System.out.println(str);
return true;

Shouldn't it output as true as well since true || true = true?

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

Re: About Question enthuware.ocajp.i.v8.2.948 :

Post by admin »

Please read the question and its explanation carefully. It is not the value of the expression bool1 || method1("4") that is being printed in the question.
If you like our products and services, please help us by posting your review here.

paulita
Posts: 1
Joined: Thu Feb 21, 2019 7:37 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.948 :

Post by paulita »

Hi,
i am writing regarding bool = (bool2 & method1("1")); //1 being true.
OperatorBitwise.JPG
OperatorBitwise.JPG (40.86 KiB) Viewed 8142 times
according to the snapshot from a book(see picture attached), the & operator is only true when both operands are true. Since bool2 is false and method1("1") is true results false.
Then i also don't understand why is true.
Thank you for your answer in advance!
Best,
Paula

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

Re: About Question enthuware.ocajp.i.v8.2.948 :

Post by admin »

>Since bool2 is false and method1("1") is true results false.
Yes, that is correct. The result of bool = (bool2 & method1("1")); will be false.

>Then i also don't understand why is true.
It is not true. Nowhere does it say that the result of the expression is true.

Please go through the code and the given explanation carefully and let me know if you still have any doubt.
If you like our products and services, please help us by posting your review here.

nerdchieftain
Posts: 5
Joined: Sun Mar 08, 2020 3:55 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.948 :

Post by nerdchieftain »

My textbook says that boolean type does not support bitwise operations.
Yet, we have `bool = (bool2 & method1("1"));`
It seems Java may be trying real hard to allow C++ style syntax here.

So is my book wrong, or is bool2 being implicitly cast to an integral type? If there were implicit casting, I could see them asking a question about that that gets tricky. Like does `true | 2` equal 0 or false? And if it's 0, is it a short or int or maybe even a byte?
I had thought boolean were stored as 8 bit bytes, but maybe that's a C++ thing and not a Java specification requirement.

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

Re: About Question enthuware.ocajp.i.v8.2.948 :

Post by admin »

Your textbook is correct. boolean type does not support bitwise operations. The code that you tried is not a bitwise operation either, although it does use an operator (&) that is used for bitwise operations!

& and |, when used with integral types perform bitwise operation but when used on boolean types, perform non-short circuiting logical operations.

Not sure which textbook you are using but this one by Hanumant Deshmukh explains this in detail (on Pg 123).

For other expressions that you have mentioned, what happened when you tried to compile/run them?
If you like our products and services, please help us by posting your review here.

nerdchieftain
Posts: 5
Joined: Sun Mar 08, 2020 3:55 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.948 :

Post by nerdchieftain »

Yeah Oracle Java 11 compiler revealed that

for boolean a, boolean b, and int k
a & b returns a boolean
also,
a & k is invalid

there is no implicit conversion from other types to boolean

This does work (the right operand of & is a boolean)
a & k % 2 == 1

This is just bizarre and seems to defy good reasoning. I would have chosen to use a byte to implement a boolean. I am sure they had their reasons. I am using Hanumant Deshmukh text you linked. It is Kindle, so I don't have the exact page number, but I am pretty sure I was just not in the correct section. The author mentions 4 motivations behind the choices made when defining the Java language. Principle two is to reduce the propensity for bugs, and I think this in fact does the opposite. Or at least, leads to more compiler caught errors.

Well, now I am just complaining. Moving on.

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

Re: About Question enthuware.ocajp.i.v8.2.948 :

Post by admin »

What do you find bizzare in this a & k % 2 == 1 ?

They may actually have used a byte internally to implement a boolean. It is just not reflected out to the application developer as that would amount to leaking an implementation detail. You identified reason 2 correctly for this. Using integers to implement conditionals (in c/c++) has indeed been proven to be a source of unnecessary confusion and a source of bugs.

On Kindle, | and & are explained in Section 6.1.1 Overview of Operators (Location 3283). (Just search for phrase "Non-short".)
If you like our products and services, please help us by posting your review here.

Syams123
Posts: 3
Joined: Sun Sep 26, 2021 5:12 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.948 :

Post by Syams123 »

Can someone please help me understand. I am new to java just started learning

As per my understanding ;

boolean bool1 = true;
boolean bool2 = false;
boolean bool = false;
bool = (bool2 & method1("1")); //1 False & False --> Prints False
bool = (bool2 && method1("2")); //2 False && False --> Prints False
bool = (bool1 | method1("3")); //3 True | True --> Prints True
bool = (bool1 || method1("4")); //4 True || True --> Prints True
}

So answer should be 3 and 4 right?
and whats is the use of int variable here?


Thanks

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

Re: About Question enthuware.ocajp.i.v8.2.948 :

Post by admin »

1. Did you read the explanation ? It explains exactly why 4 is not right.
You may read more about short-circuit operators in Java.

2. There is no use of the int variable in the code. It is just there to throw you off.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 50 guests