File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Initialization

 
venu gopal
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
The answer given for the following question is B and E.
Q: Which code fragments will succeed in initializing a two-dimensional array named tab with a size that will cause the expression tab[3][2] to access a valid element?
CODE FRAGMENT A:
int[][] tab = {
{ 0, 0, 0 },
{ 0, 0, 0 }
};
CODE FRAGMENT B:
int tab[][] = new int[4][];
for(int =0;i<tab.length;>
i++)
tab[i]=new int[3];
CODE FRAGMENT C:
int tab[][] = {
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
CODE FRAGMENT D:
int tab[3][2];
CODE FRAGMENT E:
int[] tab[] = { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0} };
Note: In CODE FRAGMENT B:
some code portion is not displaying
I think A is also correct. Please explain the answers. Thanks in advance.

[This message has been edited by venu gopal (edited February 06, 2001).]
 
Vijay pillai
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there,
A cannot be a valid answer as this code fragment creates an array of two rows.
int a[][]={{0,0,0},{0,0,0}};
and the requirement is three rows and two columns.
hence I think the correct ans is the last one.
correct me if Ia ma wrong
Vijay Pillai
 
Art Metzer
Ranch Hand
Posts: 241
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Venu.
Answer "A" is tempting. The point of this question, though, is for you to remember when you wish to access the elements of arrays, Java starts counting at "zero" instead of (the more intuitive) "one".
So in

the 7 is located at tab[0][0] and the 11 is located at tab[1][2]. Attempting to access tab[2][3] in this case will cause an ArrayIndexOutOfBoundsException to be thrown.
HTH, Venu.
Art
 
venu gopal
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Art,
Thaks for your reply. Explanation was excellant.
 
Tualha Khan
Ranch Hand
Posts: 287
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the option E is correct. tab[3][2] is trying to access the very last 0 in the array initialization sequence. There are 3 outer arrays, and in those three outer arrays, each has length as 3,
CODE FRAGMENT E:
int[] tab[] = { :0:{0, 0, 0}, :1:{0, 0, 0}, :2:{0, 0, 0}, :3:{0, 0, *0*} };
Here, doing tab[3][2],Iamtring to accessthe 0 in the asterisk from the inner array numbered as :3:.
Correct me if I am wrong!!!
Bye,
Tualha Khan
 
venu gopal
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tualha,
Your analysis is correct. E is also a valid answere
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic