Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes KAM India Edition Question 6.22 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "KAM India Edition Question 6.22" Watch "KAM India Edition Question 6.22" New topic
Author

KAM India Edition Question 6.22

Mahesh Bansal
Ranch Hand

Joined: Nov 28, 2000
Posts: 82
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 ?
Select the one right anshwer
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))
I think that option b and d should be the right answer but the book says says option b only.
Please explain, thanks in advance
Mahesh
Sweekriti D
Greenhorn

Joined: Jan 23, 2001
Posts: 10
even if u instantiate from c, (o instanceof b) returns true because c is a subclass of b.
therefore option b is correct as it should be an instanceof b only, not c. try compiling the code below & then changing line 11 to
b o = new b();
and see for yourself.
1. class a
2. {}
3. class b extends a
4. {}
5. class c extends b
6. {}
7. class testing
8. {
9. public static void main(String [] args)
10. {
11. b o = new c();
12. if(o instanceof b)
13. System.out.println("instance of b");
14. }
15. }
Sujit Kurtadikar
Ranch Hand

Joined: Dec 05, 2000
Posts: 68
You are right.
Option b and d are correct.
Zheng Huang
Ranch Hand

Joined: Dec 20, 2000
Posts: 49
d is incorrect. e is correct. Am I right?
 
jQuery in Action, 2nd edition
 
subject: KAM India Edition Question 6.22
 
Similar Threads
Khalid Mughal,for jdk 1.4, page 271, Question 6.23, wrong answer. Correct Me.
reference casting from Mugal
Khalid's Q6.23 pg 271
mock exam question
instanceof operator