Menu
Topics Index
...
`

Array Output787


If you need explanation Read this topic

If you need Answer Take test on this topic

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

A. Result = 12
B. Result = 604800
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 we have the concept of the 2D array which is collection of the 1D arrays,in 2D arrays length of the array refers the total number of rows in that array.in 2D length of the each row can be any size
Execution:
here we create the 2D with the 4 rows and each row has the different sizes and we take some local variables res=1 and k=0and we started the nested loops to travel through the 2D array,here we every time we are multiplying the elements in the 2D array..here in inner loop we are checking the input[k].length means every time we are increasing the k similar to i so here every element get multiplied and res is 604800

Posted by Uday Kumar    2015-02-24 09:02:25


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 row 2,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=0,j<1st array length 4,j++ so 3,5,6,7 get multiplied,result=630 i=1; j=0,j<2nd row (1d array)length ,j++ here 2,4 get multilied with result, result=630*2=1260 result=1260*4=5040 i=2, j=0,j<1,j++ here 1 is multiplied with result so result =5040 i=3,j=0,j<4th row lenth,j++ here 2,3,4,5 get multiplied so j=0,result=5040*2=10080 j=1,result=10080*3=30240 j=2,result=30240*4=120960 j=3,result=120960*5=604800 thn condition failed for both loops thn control goes out of for loop. sop prints result value=604800

Posted by Maheshwari Natarajan    2015-02-24 16:53:10


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Arun Kumar. 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:06:46


© meritcampus 2019

All Rights Reserved.

Open In App