Topics Index

Array Output784

If you need explanation Read this topic

If you need Answer Take test on this topic

Array Output784
What will be the output of the following program?
class ArrayOutput
    public static void main(String Result[])
        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[i].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 A.
It will multiply every number in the array and prints the result

Posted by Gopinath Manchikanti    2014-12-24 03:38:18


Posted by Jagadeesh Pallam    2014-12-24 04:08:20

ans a

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


j=0,j<2nd row (1d array)length ,j++
here 2,4 get multilied with result,

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
thn condition failed for both loops

thn control goes out of for loop.

sop prints result value=604800

Posted by Maheshwari Natarajan    2014-12-24 04:50:52


Posted by Yon Dereje    2014-12-24 05:12:48

This dose is now closed and the winners are Sai Ram,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Maheshwari Natarajan. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2014-12-25 05:23:00

© meritcampus 2019

All Rights Reserved.

Open In App