Tech Me More

To quench our thirst of sharing knowledge about our day to day experience & solution to techincal problems we face in our projects.

Advertise with us !
Send us an email at

Wednesday, June 18, 2014

Ocjp practice question #23

  What will be the output of the following java Program ?


Within main z is assigned 6. z is printed. Output: 6

Within doStuff z is assigned 5. DoStuff2 locally sets z to 4 (but MyScope.z is set to 4), but in Dostuff z is still 5. z is printed. Output: 5

Again z is printed within main (with local z set to 6). Output: 6 

Finally MyScope.z is printed. MyScope.z has been set to 4 within doStuff2(). Output: 4

Answer A is correct choice.


DevForce said...

When I used int z=4 in dostuff2(),I get 0 as output.why?

Akash Jain said...

Hello Venkatesh, When you use int z=4; in doStuff2() method it will create a local variable named Z in doStuff2() body which is a local variable and can not be accessed out of doStuff2(), in this case the class level variable z still have default value 0.

Hope it helps.