File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What's the answer? 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 "What Watch "What New topic

What's the answer?


Joined: May 01, 2004
Posts: 3

The answer after the self test is 15 15
but I'm not so sure about this. Can somebody tell me why? Thank you!

([C0DE][/C0DE] tags added)
[ July 19, 2004: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Assuming that a[1] should be a1[1] the program outputs 15 15 when compiled and run. What did you expect to get?

By the way, please take a look at JavaRanch's Naming Policy and change your displayed name accordingly.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Yan Lee
Ranch Hand

Joined: Sep 15, 2003
Posts: 94
The array object is created in the heap. The value of the address of the array object is passed to the fix() method. The value of an element is chaged in the method and the same address of the array object is returned back to the main method where it is assigned back to a2. So essentially a1 and a2 are pointing to the same array-object on the heap.

You have passed the address to the array object to the fix method

The first element of the array object now holds 7, so the modified array
holds {3,7,5} and you return back the address of the array. Both a1 and a2 point to the same array object on the heap.

So now when you add the elements of the array, it holds 3+7+5=15
I agree. Here's the link:
subject: What's the answer?
It's not a secret anymore!