Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

QID: 956597922130

 
Basil Shabazz
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this true/false question on JQPlus:
Given the following class definitions, the expression
(obj instanceof A) && ! (obj instanceof C) && ! (obj instanceof D)
correctly identifies whether the object referred to by obj was created by instantiating class B rather than classes A, C and D?
class A {}
class B extends A {}
class C extends B {}
class D extends C {}
I selected true. The correct answer is stated as false. The JQPlus explaination says: "Correct answer would be (obj instanceof B) && ! (obj instanceof C)".
I've tested this question by compiling all four classes and adding this main method to class B:
<CODE>
public static void main(String args[])
{
B obj = new B();
System.out.println((obj instanceof A) && !(obj instanceof C) && !(obj instanceof D));
}
</CODE>
After running B, the result is true. Since class A is a superclass of class B, shouldn't the correct answer to this question be true? I'm still a rookie at all this so I'm not sure, would love some feedback.
Thanks.
[This message has been edited by Basil Shabazz (edited December 08, 2001).]
[This message has been edited by Basil Shabazz (edited December 08, 2001).]
[This message has been edited by Basil Shabazz (edited December 08, 2001).]
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basil
Yes, it is true that it'll tell you if the obj is an instance of class B but the way it is written it will also tell you if it is an instance of A. The question states that it wants to know if it is an isntacne of B and not A, C or D. To do what they want you would have to test for specifically class B.

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Basil Shabazz
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, right. I didn't pay attention to that.
Thanks Dave!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic