• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

instanceof operator

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could anybody explain the question below;
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{}
Select all valid answers.
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)
thankx,
Lalit.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you have
.A.
| |
B D
|
C
a. is true but does not COMPLETELY identify B because it also fits C
b. is not true because it also would work if obj was an A
c. this is it
d. both B's and A's fit this. Actually D fits it also.
e. a B has to be an instance of an A so this wouldn't work.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic