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

Moderator: admin

Post Reply
ETS User

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

Post by ETS User » Tue Jul 31, 2012 11:04 pm

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

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

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

Post by admin » Wed Aug 01, 2012 12:27 am

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

Guest

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

Post by Guest » Wed Aug 01, 2012 1:41 am

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:

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

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

Post by admin » Wed Aug 01, 2012 10:27 am

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

Guest

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

Post by Guest » Wed Aug 01, 2012 12:30 pm

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

AnotherGuest

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

Post by AnotherGuest » Tue Aug 14, 2012 5:50 am

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

Svetopolk

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

Post by Svetopolk » Sat Nov 17, 2012 10:07 am

I am also confused as AnotherGuest.
I think that the expression should return true if obj = new B()

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

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

Post by admin » Sun Nov 18, 2012 7:12 am

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

Svetopolk
Posts: 22
Joined: Sun Nov 18, 2012 1:51 am
Location: Moscow
Contact:

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

Post by Svetopolk » Sun Nov 18, 2012 8:04 am

Oh! thanks

shining_dragon
Posts: 14
Joined: Sat Mar 01, 2014 9:12 am
Contact:

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

Post by shining_dragon » Mon Mar 03, 2014 12:58 am

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

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

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

Post by admin » Mon Mar 03, 2014 2:52 am

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

shining_dragon
Posts: 14
Joined: Sat Mar 01, 2014 9:12 am
Contact:

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

Post by shining_dragon » Mon Mar 03, 2014 4:26 am

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?

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

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

Post by admin » Mon Mar 03, 2014 5:00 am

Yes, that is correct.
If you like our products and services, please help us by posting your review here.

vchhang
Posts: 36
Joined: Tue May 06, 2014 8:30 am
Contact:

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

Post by vchhang » Tue May 06, 2014 12:04 pm

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?

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

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

Post by admin » Tue May 06, 2014 12:49 pm

No, you might want to try it out.
If you like our products and services, please help us by posting your review here.

phogi.java
Posts: 3
Joined: Sun Oct 26, 2014 9:31 am
Contact:

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

Post by phogi.java » Wed Oct 29, 2014 5:01 am

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.

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

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

Post by gparLondon » Sun Feb 08, 2015 7:57 am

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

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

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

Post by admin » Sun Feb 08, 2015 9:09 am

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

dkasti
Posts: 1
Joined: Wed Aug 12, 2015 1:12 am
Contact:

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

Post by dkasti » Wed Aug 12, 2015 1:16 am

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.

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

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

Post by admin » Wed Aug 12, 2015 1:32 am

Not sure what you are implying here. Your situation does not contradict anything given in the question.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 7 guests