Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to check the class of an Object given its reference?

 
vadiraj vd
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
here's question from Mughal's mock exam.
CODE
-------
<pre>

Given the following class definitions, which expression
identifies whether the object referred to by obj was
created by instantiating class B rather than classes A, C and D?
class A {}
class B extends A {}
class C extends B {}
class D extends A {}
a. obj instanceof B
b. obj instanceof A && !(obj instanceof C)
c. obj instanceof B && !(obj instanceof C)
d. obj instanceof C | | (obj instanceof D)
e. obj instanceof A && !(obj instanceof C) && !(obj instanceof D)

</pre>
I think the answer is c. Please correct me if I'm wrong in this.

------------------
Regards
---------
vadiraj

*****************
There's a lot of I in J.
*****************
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have the answers in front of me but I would agree with you that it is C.
Here is my reasoning:
A. - False: In this case it could be B or C.
B. - False: In this case it could be A, B, or D
C. - True: if obj is not a C and is a B then it must only be B.
D. - False: In this case it could be A, B, C, or D.
E. - False: In this case it could only be an A.
Bill
[This message has been edited by bill bozeman (edited January 12, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic