Topics Index

Array Output792

If you need explanation Read this topic

If you need Answer Take test on this topic

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

A. Result = 604800
B. Result = 12
C. Result = 15
D. Compilation Error
E. 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 we have the 2D array concept which is the collection of the 1D arrays and in 2D array each row may have different sizes and num of rows in a 2D array referes the size of the array by using nested loops we can travel through the 2D array, indexes are ranges from the o-size-1, if we exceeds this size than they JVM will through the  ArrayIndexOutOfBoundsException

here we create the int 2D array with the 4 rows and each row has tthe different sizes and we tool an local variables res assigned with the 1
next we started the nested loops for travelling through the array in the outer loop we put the condition as 0 to till it reaches the all rows and inner loop we put the condition j=0 and j<input[0].length means j<3 here it will work for 1st and last column in the middle rows we have the less size means less than in the 2nd valied indexes are 0-1 and in the 3rd valid indexes are 0 but in that rows also we need to travel till the 3 so here JVM will throw an ArrayIndexOutOfBoundsException

Posted by Uday Kumar    2015-03-06 03:44:12

ANS is E (arrayIndexIutOfBounds)

--Here,the concept of 2D ARRAY is used.

--array is declared with 4 rows and different no of elements in each row. It is also called as irregular 2-dimensional array.

--Here nests for loops are used in which outer loop runs till the length of Input array I.e. 0 to 3 and for each iteration inner loop runs till the length of 0th row of input array I.e. 0 to 3 as there are 4 elements in 0th row of input array.

--But there are only two elements in 1st row and we try to access the element 2 and 3 which throws arrayIndexIutOfBounds Exception.

Throws arrayIndexIutOfBounds Exception

Posted by Mânïshå Mùlchåndânï    2015-03-06 08:41:38

This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Mânïshå Mùlchåndânï, Sai Ram,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Sai Ram. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2015-03-09 01:43:43

© meritcampus 2019

All Rights Reserved.

Open In App