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

DanChisholm mock exam question

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DanChisholm mock exam
HI ALL

here is my question.why the answer is Bird,Cat.??
r2 =r1;// in this statement we are assigning r1 to r2 right???




class GFC301 {
private String name;
public GFC301(String name) {this.name = name;}

public void setName(String name) {this.name = name;}
public String getName() {return name;}
public static void m1(GFC301 r1, GFC301 r2) {
r1.setName("Bird");
r2 = r1;
}
public static void main (String[] args) {
GFC301 pet1 = new GFC301("Dog");
GFC301 pet2 = new GFC301("Cat");
m1(pet1,pet2);
System.out.println(pet1.getName() + "," + pet2.getName());
}}

What is the result of attempting to compile and run the program?

a. Prints: Dog,Cat
b. Prints: Dog,Bird
c. Prints: Bird,Cat
d. Prints: Bird,Bird
e. Run-time error
f. Compile-time error
g. None of the above
 
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you pass a reference to a method you pass a copy of original reference.

so,

r1 also refers to pet1 but it is a copy (a additional reference)
such that r1 and pet1 both refers to same object which has name="Dog"

pet - -- > name = dog
r1 (refers to same object)

now when you change name = "bird"
remember both pet and r1 refers to same object.

now you assign r2=r1

here only r2 reference changes and it now refers to r1 so inside method m1
if you say r2.name than it will give bird.

remember r2 is a copy of reference pet2,
pet2 reference still refers to original object where name is cat.

hope this helps,
graphics may help but I am unable to draw something over here.

bye.
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In m1() method as u said we are assigning r1 to r2.But values(object reference to r1 or r2) are not returned from the method.They are created inside a method & lost its value once the method is exited.But if internal state is modified it will be reflected in a caller method.

Revert me if ur doubt is not clear!!

Regards,
Priya.
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is how I look at it.

Before the call to m1():

pet1-->Dog
pet2-->Cat

Inside m1() before call to setName():

r1, pet1-->Dog
r2, pet2-->Cat

Inside m1() after call to setName(), before assignment:

r1, pet1-->Bird
r2, pet2-->Cat

Inside m1() after assignment:

r1, r2, pet1-->Bird
pet2-->Cat

After exiting m1()

pet1-->Bird
pet2-->Cat

So like the others have said, since the reference is copied, when you do the re-assignment, it doesn't affect the original reference (pet2).
 
naraharirao mocherla
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everybody for your replies.
I'm trying to go though each of these posts.
 
naraharirao mocherla
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So if you want to get the ouptut like Bird,Bird. Do we have to call r2.setName("Bird") also...>Is it true?.
Thank you very much for your explanations.
 
Santana Iyer
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes r2.setName("Bird"); will do but

you must call

r2.setName("Bird");
then
r2=r1;

see r2 and pet2 both refer to object which has Name - >"abcz"
now when you say r2.setName("Bird")
so
r2, pet2 ---> "Bird"
now you can change r2.

however if you do,
r2=r1
now here r2, r1, pet1 all refer to same object (although each is individual reference).
r2.setName("Bird")

now pet2 is not affected will remain same as original.

you can try both above combination

also try this
r2=r1;

r2.setName("XY");

now pet1, r1,r2 all have XY
 
naraharirao mocherla
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 Santana
i got ur reply.please bear with me. i'm working on it.
just now i tried as you advised to do.
called r2.setName("Bird") before the assignment r2=r1;

then got Bird ,Bird as the output.. that's fine..
when i called r2.setname("Bird") after the r2=r1 statement it was like the original answer i.e Bird,cat...

//then i tried this
r2=r1;
then i changed the name to "XY" i.e r2.setName ("XY"). then i got like XY,Cat.
i got it .But still i have a question
how do you know that all the r1,r2 and pet1 have the same name.
Can we call getName() method on r1 and r2 references also to know what actually they are??

thanks in advance
 
reply
    Bookmark Topic Watch Topic
  • New Topic