Page 1 of 1
About Question enthuware.ocpjp.v7.2.1489 :
Posted: Tue Apr 29, 2014 7:25 am
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?
Re: About Question enthuware.ocpjp.v7.2.1489 :
Posted: Tue Apr 29, 2014 9:52 am
by admin
Yes, it is correct. Please try it out. As the explanation says, option 3 and option 4 are actually same.
HTH,
Paul.
Re: About Question enthuware.ocpjp.v7.2.1489 :
Posted: Fri Dec 30, 2016 2:10 pm
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.
Re: About Question enthuware.ocpjp.v7.2.1489 :
Posted: Wed Nov 29, 2017 5:56 am
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
Re: About Question enthuware.ocpjp.v7.2.1489 :
Posted: Wed Nov 29, 2017 6:22 am
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.
Re: About Question enthuware.ocpjp.v7.2.1489 :
Posted: Sat Oct 13, 2018 10:35 pm
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.
Re: About Question enthuware.ocpjp.v7.2.1489 :
Posted: Sat Oct 13, 2018 11:05 pm
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.