info@meritcampus.com    +91-85006-22255
...
`

Array Output769
What will be the output of the following program?
`class ArrayOutput{    public static void main(String aRGS[])    {        int[][] scores = new int[3][];        scores[0] = new int[3];        scores[1] = new int[4];        scores[2] = new int[3];                for(int i = 0; i < scores.length; i++)        {            for(int j = 0; j < scores.length; j++)            {                scores[i][j] = i + j;                System.out.print(scores[i][j] + " ");            }        }    }}`

 A. 0 1 2 1 2 3 4 2 3 4 B. 0 1 2 4 7 11 16 22 30 37 C. 0 1 2 1 2 3 2 3 4 D. Compilation Error E. Throws ArrayIndexOutOfBoundsException
Topic:

### User comments below.All of them might not be correct.

Ans is C..here we have the concept of the 2D array which are collection of the 1D arrays..num of rows in the 2D array indicates the length oof the 2D Array..and in 2D array each row can have the different lengths

Execution:
here we create the 2D array with the size 3 means we can create the 3 rows in it,next we are creating the 1D arrays in each row with the different sizes,next using the nested for loops we are inserting the elements into the 2D array..here in the inner loop we are travelling till the 2 index only than in row we have the size the 4 so here last element get skipped,every time we are inserting row_index+col_index and next we are printing
0 1 2
1 2 3
2 3 4

Posted by Uday Kumar    2015-03-02 03:39:47

Answer is C.....We have declared a 2-d array which is an array of 1-d arrays with lengths 3,4,5....The outer for loop iterates over for 3 times and so is the inner loop...
Therefore, a total of 3*3 = 9 times,statements inside the loops gets executed

based on i and j values in each iteration,here is how the values are printed
In the first time  ----> 0+0=0
In the second time  ----> 0+1=1
In the third time  ---->  0+2=2
In the fourth time  ----> 1+0=1
In the fifth time  ---->  1+1=2
In the sixth time  ---->  1+2=3
In the seventh time  ---->  2+0=2
In the eighth time  ---->   2+1=3
In the ninth time  ---->   2+=42

Posted by Shashanka Mogaliraju    2015-03-02 09:11:58

This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Shashanka Mogaliraju,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Maheshwari Natarajan. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-03-03 03:32:24