Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How to check the class of an Object given its reference? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How to check the class of an Object given its reference?" Watch "How to check the class of an Object given its reference?" New topic
Author

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

vadiraj vd
Ranch Hand

Joined: Dec 15, 2000
Posts: 68
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.
*****************


Regards<BR>---------<BR>vadiraj<P><BR>*****************<BR>There's a lot of I in J.<BR>*****************
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to check the class of an Object given its reference?
 
Similar Threads
PGJC mock exam question
instanceof operator
Q35-mock exam from PJ
Doubt in instanceof operator
operator instanceof confusion