This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assigning array to existing array reference. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assigning array to existing array reference." Watch "Assigning array to existing array reference." New topic
Author

Assigning array to existing array reference.

Andre Roodt
Greenhorn

Joined: Dec 16, 2003
Posts: 26
The K&B book poses the following question in the self test for chapter 1 (quation 20).
Given the following,
1. public class CommandArgsThree {
2. public static void main(String [] args) {
3. String [][] argCopy = new String[2][2];
4. int x;
5. argCopy[0] = args;
6. x = argCopy[0].length;
7. for (int y = 0; y < x; y++) {
8. System.out.print(" " + argCopy[0][y]);
9. }
10. }
11. }
and the command-line invocation,
java CommandArgsThree 1 2 3
what is the result?
A. 0 0
B. 1 2
C. 0 0 0
D. 1 2 3
E. Compilation fails
F. An exception is thrown at runtime
The correct answer is F, because according to the book you shouldn't be able to assign an array with three elements to an array with two elements.
When compiling and running the exact code, I cannot seem to generate any exceptions, regardles of how many command line arguments I use. The command line arguments are output as I entered them.
So in practice it would seem that D is the correct answer.
Any comments would be helpfull.
[ January 12, 2004: Message edited by: Andr� Roodt ]
[ January 12, 2004: Message edited by: Andr� Roodt ]
Ashok Paulraj
Ranch Hand

Joined: Jul 07, 2003
Posts: 78
JVM is primarily interested to know the size of the object to be created in the heap and that is given by the first [] in the array declaration. the second [] just gives the size of the individual single dimensional array or the length of each row in the 2-d ARRAY.
hence, u can however add 1-D array of any length to each row of the 2-D array but JVM is interested to know that u don't surpass more than 3 1-D arrays or > 3 rows in the 2-d ARRAY.
hope this might help u....
thatz why u get D as the answer and not as u expected.
~ Shalini
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
The correct answer is F, because according to the book you shouldn't be able to assign an array with three elements to an array with two elements

Hi Andre. You can reassign an array to another array (of same type) with varying lengths. What you cannot do is assign a 2-dimensional array to a 1-dimensional array. The array you are re-assigning must be of the same type and dimension.
Can you point out on what page of the book did you read this.


SCJP 1.4<br />SCWCD 1.4
Sahul Yasin
Ranch Hand

Joined: Aug 28, 2002
Posts: 48
One Dimensional array can be assigned to One Dimensional array regardless of the different array length.And...I can remember,the answer given for this Question in K&B is D only.
[ January 12, 2004: Message edited by: Yasin ]

Regards,<br />Yasin<br />SCJP,SCWCD,SCBCD
Andre Roodt
Greenhorn

Joined: Dec 16, 2003
Posts: 26
Dennis,
The question is on page 53. I am at work now and see that the answer is in fact D in the PDF chapter.
I did the question late last night and I hope I didn't read the answer incorrectly, which is possible. I'll check the hardcopy tonight but I'm sure the answer will be D as well. I probably mixed up the answer for 19 with that for 20.
My hopes of getting a mention in the errata section for the book are fading, along with my shot at fame and glory.
Thanks for the feedback guys.
Jayant Kulkarni
Ranch Hand

Joined: Dec 02, 2003
Posts: 56
Hi Andre,
Do you have pdf version of K&B?
Can you send it to me? I have searched every book stall here but I am unable to find K&B book. They told me if I order, they will get in 15days, but I dont have that much time.
My email id is tcs_prashant@yahoo.com /hotmail.com
Your help is highly appreciated.
Thanks,
Jayanta
[ January 13, 2004: Message edited by: Jayant Kulkarni ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assigning array to existing array reference.
 
Similar Threads
Sierra/Bates question re: reassigning arrays
confused
Arrays
Array
Multidimensional arrays