• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

green mock query

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
Following is a piece of code from green exam no 2.Please explain?

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;
System.out.print(v.i);
System.out.print(i); }//End of another
}

1) 10030
2) 20030
3) 209930
4) 10020
answer is 4).
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ketki
now in the following code
lets see wat happens
the ball starts rolling in the main method
where amethod is called of ObParm class
now in amethod
we are creating an object of class Valhold
and setting its member variable to 30
Hope its okie for you till now
Now here is were you may be lost
now the next line

another(v,i);


in this method we are passing the reference created in amethod
Now in java when you pass a reference variable into a method you are actually sending the path of the reference variable and you can access and change the value of the member variable of
class and it would be reflected in the method.
whereas if you change the reference variable itself and you are making any change it wont be reflected it is as you are creating a new reference variable
well lets take it in this way

v.i = 20;

here you are accessing the variable i of ValHold class this change would be reflected above in the amethod
the value of i would be set to 20 from 30


now here

v = vh;

you lost the address of the object
it is not accessible now in this method
hence the value of the variable i stays 20
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it should become a lot more clear to you if you change all the System.out.print() statements to System.out.println(). I'll give you one hint: You have three print statements that appear to give one output.

If you're still in doubt, write your own print statements as marker output.
 
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic