• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assigning array to existing array reference.

 
Andre Roodt
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 330
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sahul Yasin
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Andre Roodt
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic