• 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

Stack

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused of this Q, Please help me!!!

Q: What is the output of the following fragment.

//A class was given in the main() method:
Stack s1=new Stack();
Stack s2=new Stack();
method(s1,s2);
System.out.println("s1" + s1+" "+ "s2" + s2);
public static void method(Stack s1,Stack s2){
s1.push(new Integer(100));
s2=s1 //Line 1
}
A. Compile error.
B. Runtime error.
C. s1[100]s2[100]
D. s1[ ]s2[ ]
E. s1[100]s2[ ]
F. s1[ ]s2[100]

Answer is E, but i think C should be correct. Because in Line 1, s2 points to s1, so i think s2 should have the same elements as s1. Please throw light on this issue. Thanks a lot.


(Hysterical screams removed from topic title)
[ December 07, 2004: Message edited by: Barry Gaunt ]
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read this excerpt from "Practical Java" by Peter Haggar: Understanding that parameters are passed by value and not by reference
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
of course it is E.
Params r always passed by Value,

Here the object reference variable is passed to the method, you are free to think that it is sort of memory location but THat is JVM implementation-specific.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
R�stem-� Zal�:

I am curious: why did you reply now to this three year old post?

Thanks
 
Don't touch me. And dont' touch this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic