File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock Exam Question

 
Eishpal Dhillon
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3669
9
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • 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..
 
Pragya Prakash
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • 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

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic