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

Array Output806
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}, {5, }};        int  result = 1;        for(int i = 0; i < input.length; i++)        {            for(int j = 0; j < input[0 + 2].length; j++)            {                result *= input[i][j];            }        }        System.out.println("Result = " + result);    }}`

 A. Result = 60 B. Result = 12 C. Result = 1 D. Compilation Error E. Throws ArrayIndexOutOfBoundsException
Topic:

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

Option A will the ans .. "input" is a 2d array and result is 1
in 1st for loop the loops exit condition is "length of input i.e, 5"
in inner loop the loop exit condition is  " length of  input[2]  i.e., 1"
So o/p is product of 1st elements in inner arrays i.e., 3,2,1,2,5

which is printed in last statement as "Result = 60 "

Posted by ?????????? ?????    2015-02-04 14:19:22

result=1                                                                         given 2D array it consist 5 rows
outerloop was excuted 5 times(no of rows)
inner loop excuted only one time every iteration because condition given for inner loop is
j<a[2].length means no of elements in 2nd row is only 1 element(size is 1)
inside inner loop given statement is result=result*a[i][j];
it give first element in each row  that all elements are multiplied and store value in result variable
result=result*a[0][0]*a[1][0]* a[2][0]* a[3][0]*a[4][0];
result=result(1)*3*2*1*2*5
result is 60

Posted by Goutham Singarapu    2015-02-04 14:59:55

ans a .initially 2d array is initialized with different length size of 1d array,so it is called as array of array,int variable result is initialized with 1.here nested for loop used to access array element.outer for loop iterates length of input array which is 5( it has 5 num of 1d arrays).inner for loop iterates length of 2nd indexed array which has 1 element. so for i=0 thn result is 1*3=3,  i=1 result is 3*2=6, i=3 result is 6*1=6, i=4 result is 6*2=12, i=4 result is 12*5 =60, finally sop prints 60

Posted by Maheshwari Natarajan    2015-02-04 17:28:12

ANS IS A (Result=60)

--HERE,the concept of 2D Array is used.

--Array with name input is declared and initialized of length 5.

--Nested For Loops are used.
1)Outer Loop runs till length of input array i.e. 5
2)Inner Loop runs till the length of 2d element of array
i.e. length {1} element i.e. 1.

--Result is declared with value 1 initially.Inside this Loop result is mutiplied with input[i][j] i.e. following iterations will be performed:

1*3=3 (For i=0,j=0)
3*2=6 (For i=1,j=0)
6*1=6 (For i=2,j=0)
6*2=12 (For i=3,j=0)
12*5=60 (For i=4,j=0)

--So,Result=60 will be printed at the end.

Posted by Mânïshå Mùlchåndânï    2015-02-04 18:17:14

a

Posted by Shubham Bansal    2015-02-05 04:04:45

This dose is now closed and the winners are ?????????? ?????,  for 'First Correct Comment', Goutham Singarapu,  for 'Best Comment' and Goutham Singarapu for the 'Popular Comment'. The 'lucky liker' is Pavani Blossom. 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-05 07:33:26