• 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

Mock Exam Question

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question concerning one of Mock Exam Problems:
What is the result of compiling and executing the following program:
public class Test2{
public static void main(String[] args) {
String message="hello";
new Test2().run(message);
System.out.println(message);
}
public void run(String text) {
text += " world,"
System.out.println(text);
}
A) hello world, hello world
B) hello world, hello
-------------------------------------------------
I thought the solution should be B as the method receives a copy of the original reference. Therefore the text now points to a new object which concats the "world," string. While message reference should point to the original unmodified "hello" string object. However the answer listed is A. Could someone please inform me of where the error in my logic is!
 
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your reasoning is correct. I would advise you to run the program and see it for yourself.
-Paul.

------------------
Get Certified, Guaranteed!
http://enthuware.com/jqplus
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The old reference is made to point to the new Object. x = x+y.
The old object with "Hello" alone is discarded..
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Eishpal,
I agree with ur answer & logic.
And to test it, I compiled the code u have given.
I got the result as (B).
i.e. hello world,
hello.
I doubt if the answer given in the mock exam is correct?
Let me know if I m incorrect.
Regards,
Pragya

 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! 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