Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1241 :
Posted: Thu Oct 04, 2012 9:23 pm
by ETS User
In option 4 the explanation given in the answer is:
! ( !(o instanceof B) || (o instanceof C))
This is the complement of "(o instanceof B) && (!(o instanceof C))" prefixed with a '!'.
I wanted to know how? What are the rules of taking complement of an expressions. I think following are the rules:
1. change all the && to || and change all || to &&.
2. change !(anything) to anything.
3.change anything to !(anything).
Am I correct? Are there some more rules?
Re: About Question enthuware.ocajp.i.v7.2.1241 :
Posted: Fri Oct 05, 2012 6:03 am
by admin
You don't actually need to learn how to take complement of an expression. The explanation just tells you that this and the other option are actually same because of two negations (i.e. a not of a not).
But basically, yes, ! ( A && B) is same as (!A || !B). This is called De Morgan's law of complements. You may google it for more information but again, it is not required for the exam.
HTH,
Paul
Re: About Question enthuware.ocajp.i.v7.2.1241 :
Posted: Fri Oct 05, 2012 7:21 am
by Guest
OK thanks
Re: About Question enthuware.ocajp.i.v7.2.1241 :
Posted: Fri Oct 11, 2013 5:05 pm
by javanaut
Is the second part of
Code: Select all
!(!(o instanceof B) || (o instanceof C))
the code ever evaluated?
If object-reference o IS-A B then the first part should return true and the short-circuit OR would stop the evaluation. Then the outer not ! operator would change the value of the expression to its opposite - false.
If this line of code (LOC) short-circuits how am I sure that object-reference o is not a C?
What am I missing?
Thanks for reading and stuff guys.
Re: About Question enthuware.ocajp.i.v7.2.1241 :
Posted: Sat Oct 12, 2013 3:37 am
by admin
There is ! right before (o instanceof B). So if o is-a B, then (o instanceof B) is true and !(o instanceof B) is false. So the second expression (o instanceof C) will be evaluated, which is also false. Then the whole expression is negated again because of the outer !
Try this code:
Code: Select all
public class TestClass{
public static boolean checkB(Object o){
System.out.println("getB "+(o instanceof B));
return o instanceof B;
}
public static boolean checkC(Object o){
System.out.println("checkC "+(o instanceof C));
return o instanceof C;
}
public static void main(String[] args) {
Object o = new B();
if(
!(
!(checkB(o)) || (checkC(o))
)
)
{
System.out.println("true");
}
}
}
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1241 :
Posted: Sat Oct 12, 2013 2:19 pm
by javanaut
That code is insane. Thank-you for posting it Paul. I had fun compiling that and making an educated guess as to what the output would be.

I do not think I have see an if statement with that many parentheses.
I see now what I was misunderstanding from the question on the practice exam too.
Code: Select all
! (!(o instanceof B) || (o instanceof C))
I did not think the ! 'not operator' would change the first part of the short circuit OR expression to false, which causes the second part of the expression - o instanceof C to be evaluated. Then the other outside ! changing this value further. I think I was missing the precedence rules for operators or something and thought that the ! operator preceding (o instanceof B) would of been evaluated at a later time instead of right away inside the short-circuit expression. Thank-you again for explaining Paul.
Thank-you,
javanaut