File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q from Marcus ex-2? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q from Marcus ex-2?" Watch "Q from Marcus ex-2?" New topic
Author

Q from Marcus ex-2?

sri devij
Ranch Hand

Joined: Oct 23, 2000
Posts: 37
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 ans is given as 4.How is it possible and why?Can anybody there clear my doubt?

Thanx in advance
Edy, Yu
Greenhorn

Joined: Oct 20, 2000
Posts: 3
Where is your class ValHold?
Mary Anitha
Greenhorn

Joined: Oct 13, 2000
Posts: 23
when you pass an object reference as a parameter
the changes made to the "object" will affect the object (v.i)
but
the changes to "object reference" will not be affect it.(v will not change)
object is passed by reference
object reference is passed by value

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);

/* here after coming back from the method another v=vh does not hold good. v still points to the same old object.
But the object's i value was changed in "another" method so v.i=20 */
}//End of amethod
public void another(ValHold v, int i){
i=0;
v.i = 20; //this changes the value of i of the object that is passed
ValHold vh = new ValHold();
v = vh;// this does not change the value of v when the execution returns to amethod
System.out.println(v.i+ " "+i);
// v.i=10 as vh.i=10 ; this is true only in this method ;i=0 local variable
}//End of another
}
1) 10,0, 30
2) 20,0,30
3) 20,99,30
4) 10,0,20
So the answer is 4 .
[This message has been edited by Mary Anitha (edited October 24, 2000).]
[This message has been edited by Mary Anitha (edited October 24, 2000).]
Amit Tyagi
Ranch Hand

Joined: Oct 18, 2000
Posts: 52
Hi
I studied the code...and as per my reading the answer should be 2>20,0,30....
please correct,if I am wrong...
Thanks
Amit
Mary Anitha
Greenhorn

Joined: Oct 13, 2000
Posts: 23
Amit
I think u missed v=vh in the following method
when vh was created vh.i=10
when v is made equal to vh
v.i will have 10 but in this method only, not after returning from this method
In line 3 the value of i was changed which is reflected in the calling object.
1) public void another(ValHold v, int i){
2) i=0;
3) v.i = 20;
4) ValHold vh = new ValHold();
5) v = vh;
6) System.out.println(v.i+ " "+i);
7) }//End of another
Amit Tyagi
Ranch Hand

Joined: Oct 18, 2000
Posts: 52
Hi Anitha,
Thanks... I really missed that important point....

Bye
Amit
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Q from Marcus ex-2?