Wednesday, June 18, 2014

Ocjp practice question #23

4:54 PM

  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.


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

    1. 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.



© 2014 DieHardTechy . All rights resevered. Designed by Diehardtechy