Q11. The output of the following code will be:-
int a=4;
int b=20;
System.out.print(a/b);
Answers
Explanation:
Initially an object is created which is referred by q
and has instance variables p=10 and q=66.
- When method origin1() is invoked the parameter p
starts referring to the object referred by q. However,
p=new point(0,0) creates a new object (instance
variables p=0, q=0), and p starts referring to the new
object. Since, p is a local variable, any change to p is
not visible outside the method. Therefore, instance
variables of q do not change
- q.printpoint(), therefore, prints values of instance
variables of q which are 10 and 66 3 marks
- method origin2() is invoked and parameter p starts
referring to object referred by q. Through use of p
instance variables of object referred by q, are
modified. The change is done in the object itself.
Hence, instance variables of the object change to p=15
and q=20.
- q.printpoint() prints values of instance variables of q
which are now 15 and 20.