This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array Initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array Initialization" Watch "Array Initialization" New topic
Author

Array Initialization

venu gopal
Ranch Hand

Joined: Nov 21, 2000
Posts: 40
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

Joined: Jan 10, 2001
Posts: 45
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

Joined: Oct 31, 2000
Posts: 241
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

Joined: Nov 21, 2000
Posts: 40
Art,
Thaks for your reply. Explanation was excellant.
Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
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


SCJP2, BEA WLS 6.0, DB2 UDB 7.1
venu gopal
Ranch Hand

Joined: Nov 21, 2000
Posts: 40
Tualha,
Your analysis is correct. E is also a valid answere
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array Initialization