About Question enthuware.ocajp.i.v7.2.1241 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1241 :
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?
! ( !(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?
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1241 :
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
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
If you like our products and services, please help us by posting your review here.
-
- Posts: 22
- Joined: Wed Aug 21, 2013 12:29 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1241 :
Is the second part of 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.
Code: Select all
!(!(o instanceof B) || (o instanceof C))
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.
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1241 :
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:
HTH,
Paul.
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");
}
}
}
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 22
- Joined: Wed Aug 21, 2013 12:29 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1241 :
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.
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
I see now what I was misunderstanding from the question on the practice exam too.
Code: Select all
! (!(o instanceof B) || (o instanceof C))
Thank-you,
javanaut
Who is online
Users browsing this forum: No registered users and 198 guests