1Z0-808 · Question #16
Given the code fragment: int num[][] = new int[1][3]; for (int i = 0; i < num.length; i++) { for (int j = 0; j < num[i].length; j++) { num[i][j] = 10; } } Which option represents the state of the…
The correct answer is A. num[0][0]=10 num[0][1]=10 num[0][2]=10. new int[1][3] declares an array with 1 row and 3 columns, so num.length is 1 and num[0].length is 3. The outer loop runs once (i=0), and the inner loop runs three times (j=0,1,2), setting all three elements in that single row to 10 - giving exactly num[0][0]=10, num[0][1]=10…
Question
Options
- Anum[0][0]=10 num[0][1]=10 num[0][2]=10
- Bnum[0][0]=10 num[1][0]=10 num[2][0]=10
- Cnum[0][0]=10 num[0][1]=0 num[0][2]=0
- Dnum[0][0]=10 num[0][1]=10 num[0][2]=10 num[0][3]=10 num[1][0]=0 num[1][1]=0 num[1][2]=0 num[1][3]=0
How the community answered
(27 responses)- A74% (20)
- B15% (4)
- C7% (2)
- D4% (1)
Explanation
new int[1][3] declares an array with 1 row and 3 columns, so num.length is 1 and num[0].length is 3. The outer loop runs once (i=0), and the inner loop runs three times (j=0,1,2), setting all three elements in that single row to 10 - giving exactly num[0][0]=10, num[0][1]=10, num[0][2]=10.
B is wrong because it shows values spread across three rows (num[0][0], num[1][0], num[2][0]), which would require new int[3][1] - the dimensions are swapped. C is wrong because it implies only the first inner-loop iteration ran and left the others at their default value of 0, but the inner loop completes all three columns. D is wrong because it describes a 2×4 array, which doesn't match the declared [1][3] dimensions at all.
Memory tip: In new int[rows][cols], the first number is always the row count (num.length) and the second is the column count (num[i].length) - read it as "1 row of 3 columns," and the loop structure mirrors that exactly.
Topics
Community Discussion
No community discussion yet for this question.