File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on language Fundamentels.." Watch "Question on language Fundamentels.." New topic
Author

Question on language Fundamentels..

Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 348
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:
http://www.coderanch.com/t/236886/java-programmer-SCJP/certification/Passing-array-method
Jamal Hasanov
www.j-think.com
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:
VERION A:
public static void change_i(int i[]) {
int j[] = {2};
i = j;
}
and
VERSION B:
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
bob_towers@hotmail.com
Steven Sun
Greenhorn

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


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

Joined: May 28, 2002
Posts: 2
Guys,
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":
http://www.geocities.com/mcglonec1978/javacert/javacert.html
Corey


SCJP Tipline, etc.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question on language Fundamentels..
 
Similar Threads
Problem with passing variables to a function.
could someone explain the following code
Array modification
javaprepare.com question
Array