Menu
Topics Index
...
`

Array Output788


If you need explanation Read this topic

If you need Answer Take test on this topic

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

A. Result = 604800
B. Result = 50400
C. Compilation Error
D. Throws ArrayIndexOutOfBoundsException 
Topic: Learn Arrays And Loops

If you need explanation Read this topic

If you need Answer Take test on this topic

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

Ans is B.
here in the program we are multiplying every element of the 2D matrix except the first element of every row.... In nested for loops, the outer for loop indicates the row number of 2D matrix and the inner for loop indicates the position of the element in the respective row.. and we are multiplying the result starting from every last element of the row to the second most element... so it skips the first element in every row and prints the result of 50400...

Posted by Gopinath Manchikanti    2015-02-24 14:16:27


Answer is B....Here a 2 d array is declared....in die the for loop.. we are multiplying all the elements(whose index  gretare than 0) of each 1-d array .So,all the elements present at the 0th index of each 1-d array are not multiplied...So answer is 5*6*7*4*3*4*5 = 50400

Posted by Shashanka Mogaliraju    2015-02-24 14:24:27


ans b ,, we can declare, allocate, and initialize a a two- dimensional array at the same time by providing a list of the initial values inside nested brackets.2d array is called as array of array. here we initialized 2d array with 4 row and dif size of column value. 1st row has 3,5,6,7 column size 4 2nd row 2,4 column size 2 3rd row 1 so column size 1 4th row2 ,3,4,5 column size 4 However you have to specify two dimensions rather than one, and you typically use two nested for loops to manipulate the array elements.for i=0,j =3,j>0,j-- ,here 7,6,5 values multiplied with result so result is 210,nxt for i=1 ,j=1,j>0,j--,in this loop 4 is multipled with result 840,for i =3,jis 0 condition failed. so inner for loop not get executed,for i =3,j=3,j>0,j-- in this loop 5,4,3 values multiplied with 840,so result is 50400

Posted by Maheshwari Natarajan    2015-02-24 17:34:47


This dose is now closed and the winners are Gopinath Manchikanti,  for 'First Correct Comment', Shashanka Mogaliraju, Maheshwari Natarajan,  for 'Best Comment' and Gopinath Manchikanti 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-02-25 02:19:44


© meritcampus 2019

All Rights Reserved.

Open In App