Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

mock exam question

 
Ranch Hand
Posts: 156
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • 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: 2410
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
Nothing up my sleeve ... and ... presto! A tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic