wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan Chisholm exam - Topic Constructors 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 "Dan Chisholm exam - Topic Constructors" Watch "Dan Chisholm exam - Topic Constructors" New topic
Author

Dan Chisholm exam - Topic Constructors

shweta mathur
Ranch Hand

Joined: Sep 23, 2002
Posts: 109
Hi there,
I am trying to solve Dan Chisholm exam for "Constructors" but I am unable to follow most of the questions .
Can someone give a reference to some study material where I can know how Java treats such programs. Means- how and when static initializers and other statements in a block are called when instances of inherited classes are created.
I am giving one of such questions:
(This is the modified version of Q-2 of Dan's exam where I have removed the "j" in super to remove the error)
The output now is 261357 (Ans a).
My question is how do we get this output.

class Q {
int i = 1;
{System.out.print("1");}
static {System.out.print("2");}
Q() {System.out.print("3");}
Q(int x) {System.out.print("4");}
}
class R extends Q {
int j = 2;
{System.out.print("5");}
static {System.out.print("6");}
//R() {super(j);System.out.print("7");}// dan's question
R() {super();System.out.print("7");}// my question
}
class S {
public static void main(String[] args) {
new R();
}
}

What is the result of attempting to compile and run the program?
a. Prints: 261357
b. Prints: 261457
c. Prints: 267315
d. Prints: 267415
e. Compiler Error
f. Runtime Error
g. None of the Above


--Shweta<br />SCJP 1.4 <br />SCWCD
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Shweta,
The resource that I used to develop the questions for the initialization topic was
Chapter 12 of the Java Language Specification.

The answer is "b. Prints: Static Main Constructor".
From the above we can see that the static initialization runs just after the class is loaded and before any static method is invoked. The constructor does not run until an instance of the class is created.

The answer is "e. Prints: Static Instance Constructor".
From the above we can see that the instance initializer runs before the body of the constructor.

The answer is "b. Prints: SA SB IA CA IB CB".
The remark associated with the answer is as follows.

Static initialization of the super class occurs first followed by static initialization of the sub class. Instance initialization of the super class occurs before instance initialization of the sub class.

I hope the above is helpful.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
I was also trying to follow it, it's a VERY good resource, I tried to summerize this bit of constructors as follows:
On instantiating an object of a certain type, the static initializers of the super classes are executed first starting with the highest class in the heirarchy, then the static initializer of the class of the object itself, then because of an implicit or explicit super() call the instance initializers and the constructors of the super classes are executed in couples (instance initializer & constructor) then at the end the inistance initializer and constructor of the class of the object itself are executed.
Hope this helps..


Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Dan Chisholm exam - Topic Constructors
 
Similar Threads
Dan's Q of Assertion?
Dan's exam
Q of Dan's Mock Exam
Dan's mock question
About GC (Dan's Mock)