About Question enthuware.ocpjp.v7.2.1489 :

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

Moderator: admin

Post Reply
goblingift
Posts: 6
Joined: Mon Mar 24, 2014 5:35 pm
Contact:

About Question enthuware.ocpjp.v7.2.1489 :

Post by goblingift »

You are telling me, that the answer #4 is correct:
! ( !(o instanceof B) || (o instanceof C))

But there is a || used, which means it is not an instance of C OR is a instance of B. But it won´t verify if both things are true...Is this answer now really correct?

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

Re: About Question enthuware.ocpjp.v7.2.1489 :

Post by admin »

Yes, it is correct. Please try it out. As the explanation says, option 3 and option 4 are actually same.

HTH,
Paul.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1489 :

Post by jagoneye »

goblingift wrote:You are telling me, that the answer #4 is correct:
! ( !(o instanceof B) || (o instanceof C))

But there is a || used, which means it is not an instance of C OR is a instance of B. But it won´t verify if both things are true...Is this answer now really correct?

If you remember maths had something called de morgan's law for sets

( A ∪ B)’ = A’ ∩ B’
( A∩B)’ = A’ ∪ B’

and the the second answer is simple the negated answer of the first answer.

horst1a
Posts: 37
Joined: Mon Jun 12, 2017 2:16 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1489 :

Post by horst1a »

I am not satisfied with answer number 4, still.
Lets assume o is an object of class C.
Then the expression

! ( !(o instanceof B) || (o instanceof C))

stops after the first evaluation
! ( !(o instanceof B)
because of the || operator

The expression ! ( !(o instanceof B)
returns true (C is a B)

so the object can be an instance of class C , too.

Then: When i create an instance of class C and use the following expression

! ( !(o instanceof B) || (o instanceof C))

it returns false.
Why ?
Very strange ...for me

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

Re: About Question enthuware.ocpjp.v7.2.1489 :

Post by admin »

If o points to an object of class C, then the expression evaluation will proceed as follows -
! ( !(o instanceof B) || (o instanceof C))
! ( !(true) || (o instanceof C))
! ( false || (o instanceof C)) //since the first operand of || is false, the second operand i.e. o instanceof C will be evaluated.
! ( false || true)
! ( true)
false.

You should now work out the evaluation of the expression for other cases in a similar fashion.

Mark7777
Posts: 32
Joined: Tue Apr 12, 2016 9:19 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1489 :

Post by Mark7777 »

Your explanation makes a lot of sense, now. To simplify the original question, with respect to each statement, if o is b then it should return true, but if o is c it should return false. Right? Or am I missing something.

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

Re: About Question enthuware.ocpjp.v7.2.1489 :

Post by admin »

o is b and o is c doesn't make sense. Do you mean o is B and o is C?
You might want to check out Section 11.3.3 The instanceof operator of OCAJP Java 8 Programmer Certification Fundamentals. It explains this in detail.

Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests