aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in mock exam! 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 "doubt in mock exam!" Watch "doubt in mock exam!" New topic
Author

doubt in mock exam!

jayalakshmi charugundla
Ranch Hand

Joined: Jul 22, 2009
Posts: 57
Hi all,

When I compiled the following code, it is displaying pre post.
But I thought that the answer would be,

r1 r4 pre b1 b2 r3 r2 post. This question is from javacertifications.

Could you tell me what is the correct output?







Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

It is displaying r1 r4 pre b1 b2 r3 r2 post on my machine. What are you using to run this program??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

They will get printed surely. Perhaps you edited the question and java source by mistake ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
jayalakshmi charugundla
Ranch Hand

Joined: Jul 22, 2009
Posts: 57
I run it by using NetBeans IDE 6.7 tool. But still it is the same.

run:
pre post
BUILD SUCCESSFUL (total time: 0 seconds)

Will you clarify my doubt?


Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Don't use any IDE during SCJP preparation. Try to compile your programs from the command line...
Esther Kak
Ranch Hand

Joined: Oct 11, 2006
Posts: 51
I have a question too... (I have no problem with the output that I ran)

When the control enters main...should it now first see the print statement and print "pre" and continue with the rest of the output?
Then when it sees new C() .... call the contructor of B which in turn calls A's contructor.
Can somebody please reiterate the rules for running static/initialization block and contructors.

Thanks in advance.
Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
Ankit Garg wrote:Don't use any IDE during SCJP preparation. Try to compile your programs from the command line...



I agree with Ankit. IDEs tend to make things too easy. For learning purposes, use command line as this will make you think more about error messages etc.


Santiago
My Path to SCJP Certification My Path to SCWCD Certification
Prash Singh
Ranch Hand

Joined: Aug 23, 2009
Posts: 103

Hello every one,


I think I've figured out what's happening an how it's happening..

One thing is sure that before running the child class's constructor one of the constructors of the parent class must be executed.

When the class C is compiled,at that time B is also compiled and so is A class.
now when the class C is executed, then class B is loaded in to the memory.

Static blocks are executed, when the class is loaded in to the memory.
That's why r1 and r2 are printed first.(No constructor is executed yet!!!)

Now, the control is back to the System.out.println("pre"); statement of C class.

then "pre" is printed.

Now when C's constructor is executed, B's constructor must be executed which in turn invokes A's constructor.
As class A has a initialization block,that must be executed first before running it's constructor.

And rest is simple.

I hope it helps you.


Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Hi Esther

When the class is loaded JVM will initialize the class at any of the following conditions.

If the static method of the class is called inthis case main
If the instance of the class is created
If the static variable is assigned a value.
If the non compile time constant is accessed

So in this case class C's main method is called by JVM so it has to initialize C but since C also extends B which extends A it will initalize the super class B and A before C .

Once it initializes all the class members and run static initalizer blocks in the upper tree it will now enter into main method and print pre then we are creating C instance which will call super constructor till Object and executes the initalizer and initializer block from Object to C.

Hope it helps.


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
jayalakshmi charugundla
Ranch Hand

Joined: Jul 22, 2009
Posts: 57
Thank you all for immediate response . If I use NetBeans for any program will it display any different output? Except this which I mentioned the above one I never get any problem so far.

There is only one command in to run a program( shift+f6) in NetBeans. It does n't have any separate command to compile.

I don't know is there any option to run a program at command line with NetBeans tool.


Atwal Usha
Ranch Hand

Joined: Sep 10, 2009
Posts: 137
I don't think it should alter the output however I haven't tried the code in NetBeans. I also agree with Ankit that instead of using an IDE you should use the commandline for compiling and running the program.


Java Certification Exam Mock Tests: SCJA  SCJP 5  SCJP 6  SCJP 6 (Online Training)  SCJP 6 (Instructor Led Training)   SCWCD 5   SCBCD 5  SCEA 5
John Grabowsky
Greenhorn

Joined: Dec 30, 2008
Posts: 26
Hi all,

i have the same problem (output "pre post") in Eclipse. But output is correct when moving the definition of class B to a separat file. There are some posts on Eclipse forums about that. I cannot explain this strange behavior. Dont use IDE in exam prep. Dont put multiple classes in one file when using an IDE.

Cheers
John


John<br />SCJP 5, 97%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in mock exam!