• 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

QID: 956597922130

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, right. I didn't pay attention to that.
Thanks Dave!
 
reply
    Bookmark Topic Watch Topic
  • New Topic