permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes greenhorn needs help........ 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 "greenhorn needs help........" Watch "greenhorn needs help........" New topic
Author

greenhorn needs help........

Rowan Chattaway
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
Hi, I'm just going through the marcus green mock 2:
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.print( v.i );
}//End of amethod
public void another(ValHold v, int i){
i=0;
v.i = 20;
ValHold vh = new ValHold();
v = vh; //LineX
System.out.print(v.i);
System.out.print(i);
}//End of another
}
1) 10030
2) 20030
3) 209930
4) 10020
The answer is 4)
can someone explain this to me please?.
I thought the assignment "v = vh;" at //LineX would mean v.i would equal 10
so:
10010
?
Any help would be much appreciated!
Rowan


The early bird may get the worm, but the second mouse gets the cheese.........<br /> <br />Sun Certified Programmer for Java 2 Platform<br />Sun Certified Web-Component Developer for J2EE Platform
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Rowan,
please see the following links which deal with the very same question:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=013290
http://www.javaranch.com/ubb/Forum24/HTML/010580.html
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=011501


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

Check this thread out and follow the link to Corey's cool Flash illustrating how parameters are passed around.
Junilu
[ March 07, 2002: Message edited by: Junilu Lacar ]

Junilu - [How to Ask Questions] [How to Answer Questions]
Rowan Chattaway
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
Sorry, I should have searched previous threads for similar questions.
Cheers, I understand that now!
Rowan.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: greenhorn needs help........
 
Similar Threads
q from marcus green
Marcus Green mock test 2
parameter passing
weird call by ref question
which values will be printed?