Menu
Topics Index
...
`

Array Output780


If you need explanation Read this topic

If you need Answer Take test on this topic

Array Output780
What will be the output of the following program?
class ArrayOutput
{
    public static void main(String s[])
    {
        int[] input = {3, 5, 6, 7};
        int output = multiplyEveryElement(input);
        System.out.print("Result of multiplying every element = " + output + ".");
    }

    public static int multiplyEveryElement(int[] input)
    {
        int result = 1;
        for(int i = 0; i <= input.length - 1; i++)
        {
            result *= input[i];
        }
        return result;
    }
}

A. Result of multiplying every element = 210.
B. Result of multiplying every element = 90.
C. Result of multiplying every element = 630.
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 C (Result of multiplying every element =630)

--Here,Multiplication of array is done.

--First Array is declared as well as initialized with the elements 3,5,6,7.

--After that,function is called by passing the integer array as a argument.Inside that function FOR LOOPS runs
from 0 to inut.length-1 i.e. 4-1=3 (0 to 3).Inside that loop result variable is multiplied with every element.

-->result=1
Iteration i=0
result=result*input[0]  ----> 1=1*3 ----> 3

Iteration i=1
result=result*input[1]  ----> 3=3*5 ----> 15

Iteration i=2
result=result*input[2]  ----> 15=15*6 ----> 90

Iteration i=3
result=result*input[3]  ----> 90=90*7 ----> 630

--At the end,result is returned to "output" variable and it is printed.

Posted by Mânïshå Mùlchåndânï    2015-02-06 14:51:44


c

Posted by Shubham Bansal    2015-02-06 18:16:08


This dose is now closed and the winners are Mânïshå Mùlchåndânï,  for 'First Correct Comment', Mânïshå Mùlchåndânï,  for 'Best Comment' and Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is ?????????? ?????. 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-02-09 04:52:26


© meritcampus 2019

All Rights Reserved.

Open In App