Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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

mock exam question

 
Ranch Hand
Posts: 156
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider :
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?

Select 2 correct options
a (o instanceof B) && (!(o instanceof A))
This will retrun true even if o refers to an Object of class C.

b !((o instanceof A) || (o instanceof B))


c (o instanceof B) && (!(o instanceof C))


d ! ( !(o instanceof B) || (o instanceof C))


e (o instanceof B) && !((o instanceof A) || (o instanceof C))

How the answer is c,d?

In C, if the object is of type B then it wil definitely will be instance of A.

Please clarify my doubt
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there is a logical problem with the statement of the problem. If (o instanceof B) is true, then it will always be true that (o instanceof A).
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a (o instanceof B) && (!(o instanceof A))
False. If o is not an instance of A, o cannot be an instance of B. Hence the right part will not be true for any instance of B. (instanceof returns true for an IS-A relationship).

b !((o instanceof A) || (o instanceof B))
False. If o is not an instance of A or B, it's not an instance of B! Couldn't be simpler.

c (o instanceof B) && (!(o instanceof C))
True. If o is an instance of B but not an instance of C, o has to be an instance of B, as any instance of C will be an instance of B (and A as well).
In C, if the object is of type B then it wil definitely will be instance of A.
Did you confuse A with C?
d ! ( !(o instanceof B) || (o instanceof C))
True. I can see why it is true for all types of o: A, B and C but what if we're talking about a 10 level hierarchy?
Anyone knows a smarter answer?

e (o instanceof B) && !((o instanceof A) || (o instanceof C))
False. Once again, an instance of B will be an instanceof A.
Sashi.
P.S. This question is highly contrived and is probably better off in a math/logic book!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic