aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock Exam Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mock Exam Question" Watch "Mock Exam Question" New topic
Author

Mock Exam Question

Eishpal Dhillon
Greenhorn

Joined: Sep 25, 2000
Posts: 11
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

Joined: Sep 23, 2000
Posts: 3285
    
    7
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


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Sep 18, 2000
Posts: 42
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

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Mock Exam Question