• 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

Q35-mock exam from PJ

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following class definitions, which expression identifiers 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 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 )
e). !(obj instanceof A) && !(obj instanceof C) && !(obj instanceof D)
The Answer is (c). But I think it should (a), (b), (c), (d).
Please explain for me. I really confused why not (a) (b), it's so clearly belong to A, and B.
Thank you
hanmeng
------------------
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question was, which expression is true if and only if
the current value of the variable "obj" was originally
created by "new B()".
Answer (a) is wrong because this will print "true"
obj = new C();
System.out.println(obj instanceof B)
Answer (b) is wrong because this will print "true"
obj = new A();
System.out.println(obj instanceof A && !(obj instanceof C ))
Answer (d) is wrong because this will print "true"
obj = new A();
System.out.println(!(obj instanceof C | | obj instanceof D ))
Finally, answer (e) is wrong because this will print "false"
obj = new B();
System.out.println(!(obj instanceof A) && !(obj instanceof C) && !(obj instanceof D))

That is the formal answer. The intuitive answer is:
The question is asking whether obj is a DIRECT INSTANCE
of B. Direct instances of C are INDIRECT instance of B;
we don't want to answer True for them.
The "instanceof" operator is true for DIRECT OR INDIRECT
instances. So it doesn't tell you what you want to know,
not all by itself. You have to eliminate the indirect cases
to get the right answer.
In the real world, you would never approach things this way.
You'd use the getClass() method and match that.
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We are asked to identify an expression which tells us that the object referred to by obj was instantiated from class B and not from class A or C or D. Let's examine the alternatives.
a). obj instanceof B
objects instantiated from class B and class C pass this test
b). obj instanceof A && !(obj instanceof C )
Objects instantiated from class A, class B and class D pass this test
c). obj instanceof B && !(obj instanceof C )
Only objects instantiated from class B pass this test.
d). !(obj instanceof C | | obj instanceof D )
Objects instantiated from class A and class B pass this test
e). !(obj instanceof A) && !(obj instanceof C) && !(obj instanceof D)
None of the objects created from classes A,B,C or D pass this test. The expression (Obj instanceof B) implicitly tellus us (Obj instanceof A) since B is a subclass of A.
 
reply
    Bookmark Topic Watch Topic
  • New Topic