• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

KAM India Edition Question 6.22

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given three classes A, B and C, where B is a subclass of A and C is a subclass of B, which one of these boolean expressions correctly identifies when an object o has actually been instantiated from class B as opposed to from A or C ?
Select the one right anshwer
a) (o instanceof B) && (!(o instanceof A))
b) (o instanceof B) && (!(o instanceof C))
c) !((o instanceof A) | | (o instanceof B))
d) (o instanceof B)
e) (o instanceof B) && !((o instanceof A) | | (o instanceof C))
I think that option b and d should be the right answer but the book says says option b only.
Please explain, thanks in advance
Mahesh
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
even if u instantiate from c, (o instanceof b) returns true because c is a subclass of b.
therefore option b is correct as it should be an instanceof b only, not c. try compiling the code below & then changing line 11 to
b o = new b();
and see for yourself.
1. class a
2. {}
3. class b extends a
4. {}
5. class c extends b
6. {}
7. class testing
8. {
9. public static void main(String [] args)
10. {
11. b o = new c();
12. if(o instanceof b)
13. System.out.println("instance of b");
14. }
15. }
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right.
Option b and d are correct.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
d is incorrect. e is correct. Am I right?
 
It's weird that we cook bacon and bake cookies. Eat this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic