Page 1 of 1

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

Posted: Tue Jul 31, 2012 11:04 pm
by ETS User
Given the following class definitions, the expression

Code: Select all

 (obj instanceof A) && ! (obj instanceof C) && ! (obj instanceof D)  

correctly identifies whether the object referred to by obj was created by instantiating class B rather than classes A, C and D?   

Code: Select all

class A {}   
class B extends A {}   
class C extends B {}   
class D extends C {}
---
This is one of the questions on the first exams from the software. Awesome test engine by the way... :ugeek:

Can someone explain to me how this expression is evaluated? Does the code 1) test (obj instanceof A) first then if this is true it 2) checks !(obj instance of C) and then when this returns false the code moves to the next line? I am confused by the combined && statements...

Also, I think it would need parentheses around the entire statement and should be preceded by an if keyword...

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

Posted: Wed Aug 01, 2012 12:27 am
by admin
You got it right. (obj instanceof A) will be evaluated first. Now, since && is a short circuit operator, if the value is false, it will not evaluate the right hand side expression. But if it is true, it will have to evaluate the expression on the right hand side of &&.

Try it out with & instead of && as well.

Glad to know that you liked the s/w :)

HTH,
Paul.

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

Posted: Wed Aug 01, 2012 1:41 am
by Guest
Well I am familiar with short-circuit operators... just confused by two of them in the same expression.

basically the expression is

Code: Select all

blah1 && blah2 && blah3 
I am confused how this is evaluated. If blah1 is false then blah2 is evaluated then is the result of that compared to blah3 or do they all have to be true in this situation? I am not sure how to describe this... very confusing to me right now.

Thank-you Paul for you help. :mrgreen:

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

Posted: Wed Aug 01, 2012 10:27 am
by admin
If blah1 is false then the rest will not be evaluated because && is a short circuit operator.

Not sure what you mean by "do they all have to be true in this situation?"

They can be true or false independent of each other. The value of the whole expression will depend on what their values are. The way it is does is by building a truth table (Not relevant for the exam).
HTH,
Paul.

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

Posted: Wed Aug 01, 2012 12:30 pm
by Guest

Code: Select all

public class ConfusingExpression { 
	public static void main(String[] args) { 
		
	boolean blah1 = true;
	boolean blah2 = true;
	boolean blah3 = true;
	
	if (blah1 && blah2 && blah3) { 
		System.out.println("we have a blah...");
	}
	else { 
		System.out.println("we don't have a blah...");
		}
	}
}
Hello Paul,

Thanks for the reply. I did not know about truth-tables until now. I am always confused by the math as seen on the web-page you linked to. Well anyway, I wish I could know everything but anyway again :lol:

Above I made a program to try and figure out how the combined short-circuit operators would work. The program only prints "we have a blah..." when all the boolean values are true. Otherwise it prints "we don't have a blah..." when any boolean variable in the expression is false.

All the best,

javanaut

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

Posted: Tue Aug 14, 2012 5:50 am
by AnotherGuest
Hi,

When I run the following code it prints true,

Code: Select all

        class A {}   
        class B extends A {}   
        class C extends B {}   
        class D extends C {}
        
        B obj = new B();
        Boolean randomQ = (obj instanceof A) && ! (obj instanceof C) && ! (obj instanceof D);
        System.out.println(randomQ);
Surely B is an instance of A, and the '!' operators signify that if (obj instanceof C) returns false, then !(obj instanceof C) returns true...no?

Cheers

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

Posted: Sat Nov 17, 2012 10:07 am
by Svetopolk
I am also confused as AnotherGuest.
I think that the expression should return true if obj = new B()

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

Posted: Sun Nov 18, 2012 7:12 am
by admin
As the explanation explains, the given expression returns true even if the object is an instance of A. But we don't want that. The question specifically asks to test for B. It should return true only if the object is of type B and not of A, C, or D.

HTH,
Paul.

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

Posted: Sun Nov 18, 2012 8:04 am
by Svetopolk
Oh! thanks

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

Posted: Mon Mar 03, 2014 12:58 am
by shining_dragon
Hi all, I don't understand the question here,

" correctly identifies whether the object referred to by obj was created by instantiating class B rather than classes A, C and D? " is not clear to me.

Is the question wants to know if I created an object B and passed to this code,
(obj instanceof A) && ! (obj instanceof C) && ! (obj instanceof D)

it will return true?

Because if so, then the answer is true since:
(b instanceof A) && !(b instanceof C) && !(b instanceofD)
true && !false && !false = true

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

Posted: Mon Mar 03, 2014 2:52 am
by admin
shining_dragon wrote:Hi all, I don't understand the question here,

" correctly identifies whether the object referred to by obj was created by instantiating class B rather than classes A, C and D? " is not clear to me.

Is the question wants to know if I created an object B and passed to this code,
(obj instanceof A) && ! (obj instanceof C) && ! (obj instanceof D)

it will return true?
Yes, but it should return true only if the object is of type B and not of A, C, or D. Please go through the explanation and the discussion above.

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

Posted: Mon Mar 03, 2014 4:26 am
by shining_dragon
Hi admin. Really thank you for answering my questions here and on other threads :) I really appreciated it.

So does it the answer is false because instantiating B and also A would correctly return true?

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

Posted: Mon Mar 03, 2014 5:00 am
by admin
Yes, that is correct.

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

Posted: Tue May 06, 2014 12:04 pm
by vchhang
The explanation states the correct way is:

excerpt:
Correct expression would be (obj instanceof B) && ! (obj instanceof C). This will return true only if obj points to an object of class B and not of A, C, or D.

Wouldn't this also return true if the obj points to an object of class A as well?

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

Posted: Tue May 06, 2014 12:49 pm
by admin
No, you might want to try it out.

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

Posted: Wed Oct 29, 2014 5:01 am
by phogi.java
Another way to look at this question is that:

The given expression will never evaluate anything after the first && regardless of the reference type of obj, because the first instanceof will always evaluate to true (if testing A, B, C, or D).

So it is not useful if you want to identify whether obj was created by instantiating class B.

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

Posted: Sun Feb 08, 2015 7:57 am
by gparLondon
Hi,

Is there any quick way of solving this? I mean how I solve this was created an object of b and started to evaluate it through the expression, which resulted into true and went for true, didn't realise, this expression will result true for object A as well. Just looking for small help to solve this with some trick without loosing much time in the real exam.

With respect,
GPAR

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

Posted: Sun Feb 08, 2015 9:09 am
by admin
I am not aware of any trick but from experience I can say that no trick in necessary. It is based on just one basic concept, which is, "an object of a subclass is also an object of a super class but an object of superclass is not an object of subclass" i.e. an object of class C is an object of class B (and A), but an object of A is not an object of B, or C. If you just keep this mind, the expression is actually quite simple.

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

Posted: Wed Aug 12, 2015 1:16 am
by dkasti
How about this situation:

Code: Select all

class A {}
class B extends A {}
class C extends B {}
class D extends C {} 

class K extends B{}

public class InstanceOfTest2{
	public static void main(String... ram){
		A obj = new K();
		System.out.println((obj instanceof B) && ! (obj instanceof C));

	}
}
This will print true even if the object obj is not created instantiating B, rather by instantiating from K.

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

Posted: Wed Aug 12, 2015 1:32 am
by admin
Not sure what you are implying here. Your situation does not contradict anything given in the question.

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

Posted: Sat May 09, 2020 12:40 am
by swarna pakeer
" Correct expression would be (obj instanceof B) && ! (obj instanceof C). This will return true only if obj points to an object of class B and not of A, C, or D."
but here also if obj is an object of B it is object of A also right? as B is subclass of A . In that case how can something be an object of B but not A

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

Posted: Sat May 09, 2020 4:59 am
by admin
Yes, object of B is an object of A. But object of A is not an object of B. You want an expression that identifies an object of B but not just of A.
You want to distinguish between [new B] vs [new A, new C, and new D].