3. Whats the output of the following program. (4 Marks)
int main()
{
int b[][] = {{10,20},{30,40}};
int t, q;
for (t = 0; t < 2; t++)
for (q = 0; q < 2; q++)
printf("%d ", a[t][q]);
return 0;
}
Answers
Answer:
Hey follow me first I will answer you
Explanation:
Answer:
10 66 1 mark if correct reason given
15 20 1 mark if correct reason given
Reason:
- 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. 2 marks
I hope it will help you