Forums Register Login

Green mock 2, #51

+Pie Number of slices to send: Send
I remember I saw someone post asking about Green mock 2, #51 on the past Sunday. And the other guy gave a specific explaination after that. However I can not find it anymore in this column. Anybody can tell me where the post is moved to? or who is find enough to post it again. I will paste the question once again as following. Thanks a lot.
Indy
Question 51)
Given the following code what will be the output?
class ValHold{
public int i = 10;
}
public class ObParm{
public static void main(String argv[]){
ObParm o = new ObParm();
o.amethod();
}
public void amethod(){
int i = 99;
ValHold v = new ValHold();
v.i=30;
another(v,i);
System.out.println(v.i);
}//End of amethod
public void another(ValHold v, int i){
i=0;
v.i = 20;
ValHold vh = new ValHold();
v = vh;
System.out.println(v.i+ " "+i);
}//End of another
}
1) 10,0, 30
2) 20,0,30
3) 20,99,30
4) 10,0,20
the answer is 4).
+Pie Number of slices to send: Send
Thing that U need to understand First is that
Primitive Types are Passed by Values ..
Objects are passed by references but this references are passed as such by value again ...
So when any primitive values are passed to a method , any change of that value in that method is not going to get reflected to the passing method... Same applies to Reference values...
Now when u pass a Object its contents can be changed this change is reflected the passing method...
For example ,
class A {
int i = 0 ;
public static void main(String args[])
{
int j = 40 ;
A a = new A() ;
System.out.println(a.i+ " " + j) ;
passMethod(a , j) ;
System.out.println(a.i+ " " + j) ;
}
public static void passMethod(A obj , int primitive)
{
obj.i = 20 ;
primitive = 40 ;
}
}
Same explanation applies to the ur code..
Hope this clarifies ur question....
Thanks ,
Suresh R
+Pie Number of slices to send: Send
thanks for the reply, I pretty much understand what you were saying about value. And based on this, I can go along with the first two print out in #51. since v has been assigned by a new reference of ValHold, including v.i. so, v.i is printed out to 10. and i has been given to a new value within the method another. so, it's 0. THEN, what about the second v.i print out?
which reference it is refering?? according to the answer, it is refered, not to the original one, but the one right before v was assigned by a new reference again. How can I tell which reference it is?
Thanks in advance.
Indy
+Pie Number of slices to send: Send
You might have to understand also that the v.i that is printed inside the another() method is the original v that is created within this same method. If we agree here then we should also agree that it is this same v that is passed with the call another(v,i).
when this object is passed to another, you will notice that is is changed as v.i = 20 inside the another method, and this alteration indeed does affect the original v, since no new instance of it has been created yet.So this is the 20 that is printed inside amethod.
Below this assignment there is a call to ValHold vh = new Valhold then v = vh, here now it is a completely different object since a new instance has been created through new Valhold and what happens thereafter will not affect the original v that came into this method.
I hope this will help you understand, somehow.
+Pie Number of slices to send: Send
YES, I got it. thank you very much.
Indy
Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 728 times.
Similar Threads
greenhorn needs help........
Marcus Green mockexam #2 Q51
Marcusgreen Mock Exam1 Question 51
Exam on 9th july -- pl help
A funny Mock Question! Please help me to solve.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:33:25.