aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question from KB Book 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 "question from KB Book" Watch "question from KB Book" New topic
Author

question from KB Book

Jart Bo
Ranch Hand

Joined: Oct 22, 2007
Posts: 144
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.


SCJP, SCWCD
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
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


SCJP, SCWCD, SCBCD
Jart Bo
Ranch Hand

Joined: Oct 22, 2007
Posts: 144
Thanks Deepak for the wonderful explanation!
Arjun Cheng
Greenhorn

Joined: Jan 08, 2008
Posts: 13
Thanks I learned some. It clarified.
Abhishek Chattopadhyay
Greenhorn

Joined: Jan 10, 2008
Posts: 4
Great explanation there.Really helped
Abhishek Chattopadhyay
Greenhorn

Joined: Jan 10, 2008
Posts: 4
Great explanation there.Really helped
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Chee Thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: question from KB Book