IntelliJ Java IDE
The moose likes Mock Exam Errata and the fly likes Marcusgreen Mock Exam1 Question 51 Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Mock Exam Errata
Reply Bookmark "Marcusgreen Mock Exam1 Question 51" Watch "Marcusgreen Mock Exam1 Question 51" New topic
Author

Marcusgreen Mock Exam1 Question 51

Santoo
Greenhorn

Joined: Aug 04, 2000
Posts: 6
Can anyone please explain the code
answer for this is 10,0,20
Question 51 from Marcusgreen mock exam1
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
}
Thanks in advance.
Biju Narayanan
Greenhorn

Joined: Aug 25, 2000
Posts: 4
Let us start with amethod() call. Inside amethod(), object v is created and its 'i' has a value 10. now i changes to 30.
now the another() method is up. this method copies the object reference from v of amethod() to v of another(). See at this point of time, two vs are in the memory. Both points to the same object. so changing i inside another() reflects in amethod() also. v.i changes to 20. now another obect is created and assigned to v. so v.i is 10. This is why 10 is printed first.
The other single i is just a local variable of another() and has been initialized to 0. so this prints the 0 next to 10. now
having done with another(), execution goes back to amehod().
remember that v.i has already changed to 20.
The print statement will print the last number which is 20.
Hope this helps
Biju
Santoo
Greenhorn

Joined: Aug 04, 2000
Posts: 6
Originally posted by Biju Narayanan:
Let us start with amethod() call. Inside amethod(), object v is created and its 'i' has a value 10. now i changes to 30.
now the another() method is up. this method copies the object reference from v of amethod() to v of another(). See at this point of time, two vs are in the memory. Both points to the same object. so changing i inside another() reflects in amethod() also. v.i changes to 20. now another obect is created and assigned to v. so v.i is 10. This is why 10 is printed first.
The other single i is just a local variable of another() and has been initialized to 0. so this prints the 0 next to 10. now
having done with another(), execution goes back to amehod().
remember that v.i has already changed to 20.
The print statement will print the last number which is 20.
Hope this helps
Biju

Thanks Biju,I got the answer.
 
 
subject: Marcusgreen Mock Exam1 Question 51
 
Threads others viewed
Marcus Green mockexam #2 Q51
Qn from Marcusgreen test2
passing reference, very confused, pls help
A funny Mock Question! Please help me to solve.
Exam on 9th july -- pl help
developer file tools