• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question from khalid mughal...

 
Mary Robert
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi! friends,
this is a question from khalid mughal's mock exam..
Given the following definitions, which expression identifies whether the object referred to obj was created by instantiating class B rather than classes A, C and D?
class A {}
class B extends class A{}
class C extends class B{}
class D extends class 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)
I thought the answer would be a & c. but it is given that the answer is only c. why? can anyone give a detailed explanation for this?
and one more thing is.. I scored 72% in this mock exam. Will I be
able to pass the actual exam???
Thank U
Mary.
 
Tom Tang
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Choice a only insures obj is an instance of B, but can't guarantee it's not instantiated by C, which is a subclass of B(and therefore "is a B".)
Only Choice C is Valid, because it insures that obj is NOT an instance of C, and therefore can only be instantiated by B in this case.
By the way, Here is the author's original explanation for this question in his book:

The important thing to remember is that if an object is an instance of a class, then it is also an instance of all the superclasses of this class.

[This message has been edited by Tom Tang (edited February 15, 2001).]
[This message has been edited by Tom Tang (edited February 15, 2001).]
 
Tom Tang
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Mary:
If you get that score in Khalid's test. I'm pretty sure you will get more 80% in the real test. It is widely believed that Khalid's mock is tougher (not just slightly) than the real test.
 
Mary Robert
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank U Tom.
 
Alamu Vinai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't understand the question. Can anyone explain this?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic