• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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).]
 
You are HERE! The other map is obviously wrong. Better confirm with this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic