• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Khalid's Q6.23 pg 271

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given three classes A, B, and C, where B is subclass of A and C is subclass of B, which one of these boolean expressions is true when an object denoted by reference o has actually been instantiated from class B as opposed to from A or C?
choices:
1. (o instanceof B)
2. (o instanceof B) && (!(o instanceof C))
I think both these answers are right, but the ans given is 2.
Why?
--Cathy.
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cathy Song:
Given three classes A, B, and C, where B is subclass of A and C is subclass of B, which one of these boolean expressions is true when an object denoted by reference o has actually been instantiated from class B as opposed to from A or C?
choices:
1. (o instanceof B)
2. (o instanceof B) && (!(o instanceof C))
I think both these answers are right, but the ans given is 2.
Why?
--Cathy.


I agree with you Cathy.
You can also look at it this way without thinking about the classes.
&& is a short-circuit operator. So for (2) to be true, then the 1st expression must be true, otherwise, it will immediately return false and ignore the 2nd expression. Now the 1st expression in (2) is (o instanceof B). And if this is true, then (1) must also be true.
[ October 14, 2003: Message edited by: Alton Hernandez ]
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cathy, I have the first edition (2000) of Khalid�s book.
Question 6.22 on page 213 says
Given three classes a, B and C, where B is a subclass of A and C is a subclass of B, which one of these boolean expressions correctly identifies when an object o has actually been instantiated from class B as opposed to from A or C?
(a) (o instanceof B) && (!(o instanceof A))
(b) (o instanceof B) && (!(o instanceof C))
(c) !((o instanceof A) || (!(o instanceof B))
(d) (o instanceof B)
(e) (o instanceof B) && !((o instanceof A)) || (o instance of C))
Since option (d) is true if o = new B() and if o = new C(), option (d) does not identify an object as being created from class B.
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see your point Marlene. So we have to find the correct expression, given o is an object of B and only B.
Do you think the questions in the certification exam are as tricky as this one? :roll:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic