Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes KB self test Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "KB self test" Watch "KB self test" New topic
Author

KB self test

sumaraghavi ragha
Ranch Hand

Joined: Nov 17, 2006
Posts: 118
1. class Example {
2. public static void main(String[] args) {
3. Short s = 15;
4. Boolean b;
5. // insert code here
6. }
7. }
Which, inserted independently at line 5, will compile? (Choose all that apply.)
A. b = (Number instanceof s);
B. b = (s instanceof Short);
C. b = s.instanceof(Short);
D. b = (s instanceof Number);
E. b = s.instanceof(Object);
F. b = (s instanceof String);

how the answer is D (b aslo)
But expalin how d is also correct??
subhasish nag
Ranch Hand

Joined: Apr 25, 2008
Posts: 101
because Integer, Short, Long etc all extends abstract class Number.That is why it is also instance of Number also


Thanks,<br />Subhasish
Dariusz Kordonski
Ranch Hand

Joined: Jul 11, 2008
Posts: 49
Is F not a correct answer as well? The question is about compilation and it obviously compiles without problems.
Vinod Kumar Kommineni
Ranch Hand

Joined: Jun 12, 2008
Posts: 54

F will not be correct because short and String are not in same inheritance tree ... D is correct because short is extending the Number class which is abstract and we never instantiate it ... so we don't know it exists


Regards
vinod SCJP 6.0
sumaraghavi ragha
Ranch Hand

Joined: Nov 17, 2006
Posts: 118
Thnaks all
Sandeep Bhandari
Ranch Hand

Joined: Apr 16, 2004
Posts: 201
Originally posted by Dariusz Kordonski:
Is F not a correct answer as well? The question is about compilation and it obviously compiles without problems.


The validness of instanceof operator is tried to be computed at compile time as the compiler knows the inheritance heirarchy when you say javac on command line.


SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: KB self test
 
Similar Threads
instanceof
InstanceOf
Actual parameter checking
instanceof
Multi Dimensional Arrays