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];
        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 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


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

--2nd Iteration of outer loop:-->

--3rd Iteration of outer loop:-->

--4th Iteration of outer loop:-->

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 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 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 to raise the recharge.

Posted by Merit Campus    2015-01-09 02:09:31

© meritcampus 2019

All Rights Reserved.

Open In App