About Question enthuware.ocajp.i.v7.2.1241 :

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

Moderator: admin

Post Reply
ETS User

About Question enthuware.ocajp.i.v7.2.1241 :

Post 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?

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

Re: About Question enthuware.ocajp.i.v7.2.1241 :

Post 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
If you like our products and services, please help us by posting your review here.

Guest

Re: About Question enthuware.ocajp.i.v7.2.1241 :

Post by Guest »

OK thanks

javanaut
Posts: 22
Joined: Wed Aug 21, 2013 12:29 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1241 :

Post 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.

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

Re: About Question enthuware.ocajp.i.v7.2.1241 :

Post 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.
If you like our products and services, please help us by posting your review here.

javanaut
Posts: 22
Joined: Wed Aug 21, 2013 12:29 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1241 :

Post 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. :D 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. :mrgreen:

Thank-you,

javanaut

Post Reply

Who is online

Users browsing this forum: No registered users and 243 guests