Meaningless Drivel is fun!*
The moose likes Mock Exam Errata and the fly likes Jtips mock exam Qno39 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Jtips mock exam Qno39" Watch "Jtips mock exam Qno39" New topic
Author

Jtips mock exam Qno39

gaurav nayyar
Greenhorn

Joined: Jul 13, 2001
Posts: 9
class Process {
byte b=127;
Process() {
this.methodA();
}
void methodA() {
System.out.println("Value of b is = " + b );
}
public static void main(String [] args) {
Processor p = new Processor();
}
}
class Processor extends Process {
byte b=126;
Processor() {
System.out.println("Value of b = " + b);
}
void methodA() {
System.out.println("Value of b = " + this.b);
}
}
What is the Output?
1.Prints Value of b = 0 and Value of b is = 126.
2.Compile-time error occurs.
3.Prints Value of b = 126 and Value of b = 126.
4.Prints Value of b = 127 and Value of b = 126.
the correct answer is :1
here i can't get how zero is printed ?
can anybody help me?
Bill Tripper
Greenhorn

Joined: May 30, 2001
Posts: 24
When you invoke the constructor in the staement "Processor p = new Processor();", the following events transpire.
1. If this is the first reference to class Processor, then perform any super class (Process) static initialization. Nothing is static in class Process, so nothing happens.
2. If this is the first reference to class Processor, then perform any Processor static initialization. Nothing is static in Processor, so nothing happens.
3. Inside the Processor constructor, the first thing we do is invoke the default super constructor, i.e, Process().
4. Inside the Process constructor, the first thing we do is invoke the default super constructor, i.e. Object().
5. Back from the Object() constructor with no visible effect, and currently in the Process constructor, the next thing we do is the instance initialization. This sets the Process byte b value to 127.
6. Still inside the Process() constructor, we've seen a lot of stuff done before the first statement is encountered. Now it's time to execute the Process() constructor statements, in order. That is, invoke "methodA()".
7. The "methodA" that gets invoked is from class Processor, not class Process, because we're really an object of type Processor. Even though we're deep in the bowels of the Process() constructor, polymorphism takes over and it's the Processor "methodA()" that is invoked.
8. Inside methodA(), "this.b" refers to the "b" in class Processor. This value has not been initialized yet, so we print a value of zero.
9. Return from class Processor methodA() to the Process constructor.
10. We're done with the Process() constructor so return to the Processor() constructor.
11. Back from the Process() constructor with significant visible effect, and currently in the Processor constructor, the next thing we do is the instance initialization. This sets the Processor byte b value to 126. Note how this is occurring after the value was printed from the super constructor.
12. Now we're finished instance initialization, and we execute the constructor statements in order, that is, we print out the b value of 126.
The point here is that the base class constructor is executed before the instance initialization of the derived class. Also, via methods and polymorphism, the base class can retrieve and display (or alter!) pre-instance initialization values from the derived class. The derived class instance initialzation occurs subsequent to the base class constructor. Understanding this should be worth a question or two on the certification exam.
Bill
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Wow! Great explanation Bill.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Jtips mock exam Qno39