Win a copy of Terraform in Action this week in the Cloud forum!
  • 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 Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Khalid Mughal,for jdk 1.4, page 271, Question 6.23, wrong answer. Correct Me.

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Khalid Mughal,for jdk 1.4, page 271, Question 6.23, wrong answer. Correct Me.

Question 6.23

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 is true when an object denoted bt reference o has actually been instantiated from class B as opposed to from A or C ?

Select the one correct answer.
(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 instanceof C))

Solution :

class A
{
};

class B extends A
{
};

class C extends B
{
};

class pankaj
{
public static void main(String[] args)
{
B o = new B();

System.out.println(" (a) " + ((o instanceof B) && (!(o instanceof A))));
System.out.println(" (b) " + ((o instanceof B) && (!(o instanceof C))));
System.out.println(" (c) " + (!((o instanceof A) || (o instanceof B))));
System.out.println(" (d) " + (o instanceof B));
System.out.println(" (e) " + ((o instanceof B) && !((o instanceof A) || (o instanceof C))));
}
};


Output :

(a) false
(b) true
(c) false
(d) true
(e) false

Correct Answer : Option (b) and (d)
But in the book, answer (page 531) is given as option (b).

Please tell me, where I done wrong or my answer is correct.

Pankaj Shinde
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what the question meant was :-

Object o = new B();

However, your answer is correct.
[ April 06, 2007: Message edited by: swarna dasa ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure there's nothing more to option d?
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pankaja,

I guesss..
(d) can be like this : (o instanceOf B) watch it correctly in book.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer d is clearly false. If reference o had been made as follows



Answer d would still evaluate to true. The question asks if we can tell if the object was instantiated from class B and not from class A or C. Answer d cannot distinguish between instantiation from B or C and therefore is incorrect. The question being asked was not which expressions evaluate to true.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree with Dave,

Question is specific to, which expression will correctly tell you, the instance if of class B and not of super class A and and not of child class C.

So expression,

Doing instanceof test with class A is meaningless, because A knows nothing about class B (think it, in this way). The main concern must be to test whether it is instanceof class B and not instanceof class C.


Thanks and Regards,
cmbhatt
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic