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

Array Output783
What will be the output of the following program?
`class ArrayOutput{    public static void main(String s[])    {        int[][] input = {{3, 5, 6, 7}, {2, 4}, {1}, {2, 3, 4, 5}};        int  result = 1;                for(int i = 0; i < input.length; i++)        {            for(int j = 0; j < input[2].length; j++)            {                result *= input[i][j];            }        }        System.out.println("Result = " + result);    }}`

 A. Result = 604800 B. Result = 12 C. Result = 15 D. Compilation Error E. Throws ArrayIndexOutOfBoundsException
Topic:

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

here option:B
here in main method one 2d array name input[][] created with 4rows
0th row={3,5,6,7} length{4}
1st row={2,4}    length{2}
2nd row={1}     length{1}
3rd row={2,3,4,5} length{4}
and variable result is created and it intialized with 1

after a nested loop
for outer loop  0 to 4
and for innerloop 0 to 1(a[2].length means no of element in 2nd row is 1)
and statement result=result*input[i][j];
its executes only one time with respective outerloop

1st iteration result=result*input[0][0]=>result=1*3=> result=3;

2nds  iteration result=result*input[1][0]=>result=2*3=> result=6;

3rd  iteration result=result*input[2][0]=>result=6*1=> result=3;

4rth  iteration result=result*input[3][0]=>result=6*2=> result=12;

after comming from loop the value stored in result variable is 12
(working of this code is multipying   first element in  each row)
result=12
option:B

Posted by Goutham Singarapu    2015-02-13 12:28:27

Ans is B..here we have the 2-D array concept which contains the set of 1-D arrays..
here we have the 4 rows 2-D array and using the nested loop we are travelling through the 2-D array...in the inner loop we use the condition is input[2].length which is 1 in the 3 rd row we have only 1 elements so length will b 1..so every time inner loop repeats only 1time so here we will multiply 1st element of every row than it will be like this 3*2*1*2=12

Posted by Uday Kumar    2015-02-16 03:56:10

This dose is now closed and the winners are Goutham Singarapu,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Goutham Singarapu. 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-02-16 05:09:34