What is the result?

Given:

What is the result?
A. 9 5
B. 81 25
C. Compilation fails.
D. 0 0

Download Printable PDF. VALID exam to help you PASS.

6 thoughts on “What is the result?

  1. Answer A.
    The important point here is that the int x and int y that where declared and initialized within the main() method are actually local variables (i.e. only accessible within the main() method, while the int x and int y that where declared in the second line (i.e. immediately after class definition, are basically instance fields. Therefore, no matter what happens in the code, at the end the println() method will pickup the local x and y. Therefore, it prints 9 and 5 consequently.

    Note: the initialize() method indeed reassigns the value of the instance fields x and y to 81 and 25. But those are instance fields, meaning that they belong only to the newly created object (obj). Therefore, if the println() method stated obj.x and obj.y, then it would have printed 81 and 25.

    13

Leave a Reply

Your email address will not be published. Required fields are marked *