This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
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

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mock Exam Question" Watch "Mock Exam Question" New topic

Mock Exam Question

Eishpal Dhillon

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);
public void run(String text) {
text += " world,"
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: 3592
Your reasoning is correct. I would advise you to run the program and see it for yourself.

Get Certified, Guaranteed!

Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
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,
I doubt if the answer given in the mock exam is correct?
Let me know if I m incorrect.

I agree. Here's the link:
subject: Mock Exam Question
jQuery in Action, 3rd edition