Menu
Topics Index
...
`

Array Output781


If you need explanation Read this topic

If you need Answer Take test on this topic

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

    public static int multiplyEveryElement(int[] input, int result)
    {
        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 = 0.
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...
here we have the Arrays Concept...Array is an Collection of Similar data elements stored in Adjacent memory locations....and arrays are fixed in the size....we cant change the size of array once we created...

Arrays index starts from the 0.... if we create an array with size 10 than valid Indexes wil 0-9..
array size must be always postv number..if we give ngtv num than it will throw Exception...

Coming to the program here we have an array initialized with the element 3,5,6,7.and another local variable result initialized with the 0...next we have another static method multiply() for this method we are passing array and result..passing array meand call by reference it is....so here we are iterating through the array...here result variable assigned with the 0..here we have the for loop to iterate through the array here we compound assignment operator..here we are multiplying every array element with 0..so anything multiplied with 0 is so...here every we will ger 0 only..so ans is 0

Posted by Uday Kumar    2014-12-01 12:43:59


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Goutham Singarapu. 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    2014-12-02 03:58:32


© meritcampus 2019

All Rights Reserved.

Open In App