wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes whizlab question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "whizlab question" Watch "whizlab question" New topic
Author

whizlab question

Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432


output :00
05
please explain the output at line1,2,3?

SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

You can find the order of initialization in some good Java Books (like K&B SCJP6).
But for the purpose of your question, just know that before your assignment of instance variables, they are set to default values (i.e. int = 0, Object = null, etc.).

The instance variables assignment itself happens in up-to-down manner, so when you start initializing x variable, the y variable is set to default value (which is 0). When the assignment of x variable is finished, the y variable will be assigned the given value.

Hope that following this you can understand the rest of the code.


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

Pedro Kowalski wrote:You can find the order of initialization in some good Java Books (like K&B SCJP6).
Hope that following this you can understand the rest of the code.

got your concept.
yes i am referring the same book.
this question was little tricky or my basics are still not cleared.
Sudhakar Sharma
Ranch Hand

Joined: Apr 04, 2009
Posts: 71

Hi, have you read all about instance initializers, I think a little hint you will need to solve it yourself.
First, the main() method is called, in that the constructor of the Main class which you wrote and all superclasse's contructors are invoked within this after that instance initializers and so on...........
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Arjun Srivastava wrote:
Pedro Kowalski wrote:You can find the order of initialization in some good Java Books (like K&B SCJP6).
Hope that following this you can understand the rest of the code.

got your concept.
yes i am referring the same book.
this question was little tricky or my basics are still not cleared.


These questions are tricky, even if you read about initialization process but you see this type of question for the first time.

Next question basing on default instance variable values shouldn't be a problem to you :-)
Gari Jain
Ranch Hand

Joined: Jun 29, 2009
Posts: 100
The next time you will see such a question, you will be able to solve it. This is why we practice...and this is about learning the tricks which makes even the trickiest of the questions easy.


OCPJP 6-100%; Preparing for GATE11
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

yeah ,i hope so.
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

how to get 05 05 in the output ?


hi,Arjun Srivastava can you refer me the link to mock question ?


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

mohitkumar gupta wrote:how to get 05 05 in the output ?

hi mohitkumar gupta,why don't you try out,just twist the code .

BTW,this prints 05 05
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

i still didn't get the answer.How is the program working.

x would be initialized before go is called i.e null
the y would be initialized to 5.
then what's the use of go method.

I have read about the order of initialization Kathy Sierra book but still can't get the code
please tell me how is it working ???
kirankumar reddy
Greenhorn

Joined: Jul 22, 2010
Posts: 6
mohitkumar gupta wrote:i still didn't get the answer.How is the program working.

x would be initialized before go is called i.e null
the y would be initialized to 5.
then what's the use of go method.

I have read about the order of initialization Kathy Sierra book but still can't get the code
please tell me how is it working ???


1st print statemnt in main:creates new object, x is initialized by calling go() which returns 0: output is 00

last print statement creates another object,so again x is initialized by calling go() which returns 0, and y is initialized to 5. output is 05
 
Don't get me started about those stupid light bulbs.
 
subject: whizlab question