Menu
Topics Index
...
`

Array Output786


If you need explanation Read this topic

If you need Answer Take test on this topic

Array Output786
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 = 604800
B. Result = 12
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  d.in this program initially 2d array initialized with 4 dif length of 1 d arrays.2d array is called as array of array.nested for loop is used to access an array elements.outer for iterats upto length of givn array.inner for loop condition checked with 1d array length.i=0,j=0 condition satisfied result multiplied with 3,thn k get incrementd thn j=1 ,array input[1] length 2 so condition satisfied thn result multiplied with array elements for loop's i=0 iteration completed, aft this i=1,j=0 here condition failde input[4],we are accessed invalid or outofBound element,so ArrayIndexOutOfBoundException thrown

Posted by Maheshwari Natarajan    2015-01-06 04:21:57


ANS IS A

This program is used to display the multiplication of every element of the 2D Array.

--Here,First Loop runs till the length of 2D array.Inside this loop another loop runs till the length of elements present in every row.

--1st Iteration of outer loop-res=res*input[0][0]-->res=3
res=res*input[0][1]-->res=3*5=15
res=res*input[0][2]-->res=15*6=90
res=res*input[0][3]-->res=90*7=630

--2nd Iteration of outer loop:-->
res=res*input[1][0]-->res=630*2=1260
res=res*input[1][1]-->res=1260*4=5040

--3rd Iteration of outer loop:-->
res=res*input[2][0]-->res=5040*1=5040

--4th Iteration of outer loop:-->
res=res*input[3][0]-->res=5040*2=10080
res=res*input[3][0]-->res=10080*3=30240
res=res*input[3][0]-->res=30240*4=120960
res=res*input[3][0]-->res=120960*5=604800

Posted by Mânïshå Mùlchåndânï    2015-01-06 17:19:42


ANS IS AThis program is used to display the multiplicationof every element of the 2D Array.

Posted by Shubham Bansal    2015-01-06 20:00:35


Ans is D..here we have the 2-d Array concept(Collection of 1-D Arrays),in 2-D array we may have different lengths for the each row..num of rows refer the length of an 2-D Array here we have the size 4..here we have nested loops for travelling through the 2-D Array in the inner for loop we are we are multiplying the elements of array and here we are increasing the k every time for every time it will increase so it increase more than 4 so here ArrayIndexOutOfBoundsException will thrown

Posted by Uday Kumar    2015-01-07 16:37:43


This dose is now closed and the winners are Maheshwari Natarajan,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Bolo Raven. 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-01-09 02:09:31


© meritcampus 2019

All Rights Reserved.

Open In App