• 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

instanceof

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A {}
class B extends A {}
class C extends B {}
Which of these boolean expressions correctly identifies when an object 'o' acutally refers to an object of class B and not A or C?
Options : 1) (o instanceof B)&&(!(o instanceof A))
2) !((o instance of A)|| (o instanceof B))
3) (o instanceof B)&&(!(o instance of C))
4) !(!(o instanceof B)|| (o instanceof C))
5)(o instanceof B)&&!((o instanceof A)|| (o instanceof C))
Ans : 3,4
Please explain me..
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to rephrase each line in plain English, see if it helps!
Type it and we'll see
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To list:
what is the likelyhood for such type of questions. it is like going to a interview and you are asked some tongue twister words.
what are they trying to test here ?

Originally posted by sonir shah:
class A {}
class B extends A {}
class C extends B {}
Which of these boolean expressions correctly identifies when an object 'o' acutally refers to an object of class B and not A or C?
Options : 1) (o instanceof B)&&(!(o instanceof A))
2) !((o instance of A)|| (o instanceof B))
3) (o instanceof B)&&(!(o instance of C))
4) !(!(o instanceof B)|| (o instanceof C))
5)(o instanceof B)&&!((o instanceof A)|| (o instanceof C))
Ans : 3,4
Please explain me..

 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if o is object of B then these two are true
o instanceof B (ofcourse)
o instanceof A
but o instanceof C is false
so now you can do it.

Originally posted by sonir shah:
class A {}
class B extends A {}
class C extends B {}
Which of these boolean expressions correctly identifies when an object 'o' acutally refers to an object of class B and not A or C?
Options : 1) (o instanceof B)&&(!(o instanceof A))
2) !((o instance of A)|| (o instanceof B))
3) (o instanceof B)&&(!(o instance of C))
4) !(!(o instanceof B)|| (o instanceof C))
5)(o instanceof B)&&!((o instanceof A)|| (o instanceof C))
Ans : 3,4
Please explain me..

 
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mark stone:
To list:
what are they trying to test here ?


This type of question tests whether you:
1. understand how instanceof works
2. understand how inheritance works and how
classes that belong to a hierarchy are related
3. know how logical operators work
These are all included in the objectives for the exam so there may be a possibility that you will get a question like this.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic