• 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

Kai Notes - http://www.jdiscuss.com/

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
the method is so defined:
public static void method(Stack s1,Stack s2){
s1.push(new Integer(100));
s2=s1
}
what is the output ? I choose E
A. Compile error, because ...
B. Runtime error, because ...
C. s1[100]s2[100]
D. s1[ ]s2[ ]
E. s1[100]s2[ ]
F. s1[ ]s2[100]
Need to know the methods of Stack: push(), pop() and peek().
Stack is part of the java.util package - it is a subclass of Vector so I guess it is part of the objectives.
Kai: Many thought this is a Collection question, but I believe
it is more about argument passing.
-Arun
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arun Pai:

Kai: Many thought this is a Collection question, but I believe
it is more about argument passing.
-Arun


Yes, it is a question on argument passing, but why the correct output is E, not C. Thanks for yer kind explanation.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Luco,
it's because you only
refer the local s1 to s2
so the original stays the same..

so answer would be E
Robert
[ May 12, 2002: Message edited by: Robert Ziel ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is not correct.
------------
Hi Luco,
it's because you only
refer the local s1 to s2
so the original stays the same..

so answer would be E
Robert
-------
since both will be passed by reference,changes made to s2 will also appear in main.
correct me if iam wrong
 
Robert Ziel
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Chiru,
youre wrong because you in fact you have 4 references to 2 objects
when you say s1.push(100) you
change the object1
when you say
s1 = s2
then only the local copy of s1 refers to s2
when the method ends
s1 still refers to the first object
if find it poor programming
you should use differnt var names.
robert
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some code and some commentsa to further explain this 'phenomenon'.


[ May 13, 2002: Message edited by: John Bateman ]
 
You’ll find me in my office. I’ll probably be drinking. And reading 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