• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Exam on 9th july -- pl help

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Acc to me the ans to this question is 20,0,30 . any comments??
Question 51) � NU
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 �C
Answer to Question 51)
Answer 51)
Objective 5.4)
4) 10,0,20
In the call
another(v,i);
A reference to v is passed and thus any changes will be intact after this call.
Thanks in advance
Padmini
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Padmini,
See the comments.

Hope this helps,
Vanitha.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vanitha,
In another(), your comments say that assigning vh to v means v.i == 10 because it references a new Valhold object. Yet back in amethod(), your comments say the System.out.println(v.i) will print "20" due to the changes made in another(). Does not the reference to the new ValHold object supplant the change to 20, so that System.out.println(v.i) should print "10"?
Thanks,
Chuck
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Changing the reference of v in another() does not affect the state of the original object referenced by v.
I had the same confusion, ranchers have cleared my doubt.
Take a look at this sample code,


public class RefCheck
{
String msg = "Red";
public static void changeRef(RefCheck rc)
{
System.out.println("Before changing reference " + rc.msg + " rc is " + rc);
//Before changing reference Red rc is RefCheck@70192c30
rc.msg = "Green";
System.out.println("After changing the value of the msg " + rc.msg + " rc is " + rc);
//After changing the value of the msg Green rc is RefCheck@70192c30
rc = new RefCheck();

System.out.println("After changing Reference to new object " + rc.msg + " rc is " + rc);
//After changing Reference to new object Red rc is RefCheck@72952c30

}
public static void main(String args[])
{
RefCheck ref = new RefCheck();
System.out.println("Before changeRef " + ref.msg + " ref is " + ref);
//Before changeRef Red ref is RefCheck@70192c30
changeRef(ref);
System.out.println("After changeRef " + ref.msg + " ref is " + ref);
//prints After changeRef Green ref is RefCheck@70192c30

}
}



Hope this helps,
Vanitha.

[This message has been edited by Vanitha Sugumaran (edited July 07, 2001).]
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for clearing out the oParm , valhold code.

------------------
Preparing for the Java 2 Certification exam
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic