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 Question on language Fundamentels.. 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 "Question on language Fundamentels.." Watch "Question on language Fundamentels.." New topic

Question on language Fundamentels..

Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 358
What happens when the following program is compiled and run. Select the one correct answer.

a: The program does not compile.
b: The program prints 0.
c: The program prints 1.
d: The program prints 2.
e: The program prints
The Correct answer is C, but why ???
I can se that the j array are castet to the i array, and this must be the i, that it�s created in the static methoed !
[ Added UBB [CODE] tags to preserve whitespace and improve readibility ]
[ May 27, 2002: Message edited by: Jessica Sant ]
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
Hi, Frank
You question already has discussed in this thread:
Jamal Hasanov
bob towers
Ranch Hand

Joined: May 20, 2002
Posts: 33
This is a really nice question there are two versions of it:
public static void change_i(int i[]) {
int j[] = {2}; i = j; }

-------------------------------<br />The early bird gets the worm! So if you want something else for breakfast, stay in bed.<br />-------------------------------
bob towers
Ranch Hand

Joined: May 20, 2002
Posts: 33
Sorry, i'm having keyboard problems, i'll try again:
This is a really nice question there are two versions of it:
public static void change_i(int i[]) {
int j[] = {2};
i = j;
public static void change_i(int i[]) {
i[0] = 2;
i[0] *= 2;
In version A you pass a copy of a reference to an array. Within the method body you change the value of *THE COPY* but you do not change the value of any of the contents of the *ORIGINAL* array.
In version B you use the copy of the reference of the array to address its *CONTENTS* and change their value.
If i'm not mistaken you found this question on javaprepare's site. I think these versions neatly delineate the distinction between a reference to an array and referencing the contents of the array..... or at least, i found it useful.
best wishes
Steven Sun

Joined: Apr 29, 2002
Posts: 26
I think Bob got the key!

newly SCJP2 <img src="tongue.gif" border="0">
Ashwani Mangy

Joined: May 28, 2002
Posts: 2
I know that the problem have been explained very well but I just want to add one thing. In Java parameter passing strategy is call-by-value and not call-by-reference. Call-by-reference would have allowed values in the actual parameters to be changed via formal parameters.
Formal parameter can only change the state of the object whose reference value was passed to the method.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
For a demonstration of how parameters are passed in Java, go here and click on the appropriate link under "Parameter Passing in Java":

SCJP Tipline, etc.
I agree. Here's the link:
subject: Question on language Fundamentels..
It's not a secret anymore!