• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
*****************
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic