• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question from KB Book

 
Jart Bo
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could someone please carefully explain the answer to KB Book Q#14 (Chapter 3)? My answer was F when I took the Self Test and I couldn't understand why the answer was D.

Given:
class Bird {
{ System.out.print("bl "); }
public Bird() { System.out.print("b2 "); }
}
class Raptor extends Bird {
static { System.out.print("r1 "); }
public Raptor() { System.out.print("r2 "); }
{ System.out.print("r3 "); }
static { System.out.print("r4 "); }
}
class Hawk extends Raptor {
public static void main(String[] args) {
System.out.print("pre ");
new Hawk();
System.out.println("hawk ");
}
}
What is the result?
A.pre b1 b2 r3 r2 hawk
B.pre b2 b1 r2 r3 hawk
C.pre b2 b1 r2 r3 hawk r1 r4
D.r1 r4 pre b1 b2 r3 r2 hawk
E.r1 r4 pre b2 b1 r2 r3 hawk
F.pre r1 r4 b1 b2 r3 r2 hawk
G.pre r1 r4 b2 b1 r2 r3 hawk
H.The order of output cannot be predicted.
I.Compilation fails.
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D. r1 r4 pre b1 b2 r3 r2 hawk

When main() of Hawk is invoked. Hawk extends Raptor and Raptor extends Bird.
1. Bird class is loaded and its static blocks are executed in the order of apperance if any. Bird does not have any static blocks.
2. Raptor is loaded and and its static blocks are executed in the order of apperance if any. Since Raptor does have static blocks
r1 and r4 is disaplyed. r1 r4
3. Hawk class is loaded and its static blocks are executed in the order of apperance if any. Hawk does not have any static blocks.
4. System.out.print("pre "); is executed in main and hence now the result is r1 r4 pre
5. new Hawk(); is executed and now Hawk extends Raptor and Raptor extends Bird. So first the top most parent class constructor is executed. Before the Bird() is invoked its instance intializer if any are executed in the order of appearance. And hence { System.out.print("bl "); }is executed and now the result is r1 r4 pre b1
6. Now Bird() runs and result is r1 r4 pre b1 b2
7. After Bird() runs and before Raptor() runs the instance intializer if any are executed in the order of appearance. And so { System.out.print("r3 "); } runs and result is r1 r4 pre b1 b2 r3
8. Raptor() runs and result is r1 r4 pre b1 b2 r3 r2
9. After Raptor() runs and before Hawk() runs the instance intializer if any are executed in the order of appearance. Hawk does not have instance intializer and does not have a Hawk() explicitly defined so compiler inserts Hawk() and runs it.
10 System.out.println("hawk "); runs and main() exits.
So finally result is r1 r4 pre b1 b2 r3 r2 hawk which matches optiond D D. r1 r4 pre b1 b2 r3 r2 hawk
Hope this clears
 
Jart Bo
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Deepak for the wonderful explanation!
 
Arjun Cheng
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks I learned some. It clarified.
 
Abhishek Chattopadhyay
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great explanation there.Really helped
 
Abhishek Chattopadhyay
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great explanation there.Really helped
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chee Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic