Topics Index

Array Output789

If you need explanation Read this topic

If you need Answer Take test on this topic

Array Output789
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; 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 D here we have the Arrays concept..arrays stores the similar kind of elements in the adjacent memory locations and indexes starts from the 0 to till the size-1, if we try to access the element out of the index range than we vl get the Exception and arrays are fixed in the size... and here we have the 2D array which are collection of the 1D arrays


here we have the nested loops to travel through the the 2D array, in the outer loop we started at row 0 and condition is till it completes the rows next in the inner loop we are initializing j input[i].length which gives the length but the indexes valid till the lenth-1 so here  ArrayIndexOutOfBoundsException  wil raise as it will give 4 for 1st row but valid indexes is 0-3..

Posted by Uday Kumar    2015-02-26 03:51:40

ANS IS DD ( ThrowsArrayIndexoutOfIndex)

---Here, the concept of array is used.

--Array is collection of elements starting with index 0 and length of the array indicates no of elements of array.

---Here,2D array is declared with 4 rows and different no of elements in each row I.e. that means it is irregular 2-dimesional array.

---first for loop runs from 0 till length of array I.e. loop will run 4 times.

For each iteration for loop runs from length to 0 of length is 4 then elements index will be from 0 to 3 then accessing4th element throws array index out of bounds.

Posted by Mânïshå Mùlchåndânï    2015-02-26 08:20:44

Answer is d

Posted by Shashanka Mogaliraju    2015-02-27 03:02:31

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

Posted by Merit Campus    2015-02-27 03:18:06

© meritcampus 2019

All Rights Reserved.

Open In App