• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

KB self test

 
sumaraghavi ragha
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because Integer, Short, Long etc all extends abstract class Number.That is why it is also instance of Number also
 
Dariusz Kordonski
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is F not a correct answer as well? The question is about compilation and it obviously compiles without problems.
 
Vinod Kumar Kommineni
Ranch Hand
Posts: 54
Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sumaraghavi ragha
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thnaks all
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic