About Question enthuware.ocpjp.v7.2.1489 :
Moderator: admin
-
- Posts: 6
- Joined: Mon Mar 24, 2014 5:35 pm
- Contact:
About Question enthuware.ocpjp.v7.2.1489 :
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?
! ( !(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?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1489 :
Yes, it is correct. Please try it out. As the explanation says, option 3 and option 4 are actually same.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 97
- Joined: Wed Dec 28, 2016 9:00 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1489 :
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.
-
- Posts: 37
- Joined: Mon Jun 12, 2017 2:16 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1489 :
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
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
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1489 :
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.
! ( !(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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 32
- Joined: Tue Apr 12, 2016 9:19 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1489 :
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.
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1489 :
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.
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.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 36 guests