Menu
Topics Index
...
`

Nested for loop


If you need explanation Read this topic

If you need Answer Take test on this topic

Nested for loop
What will be the output of the following program?
public class HelloWorld {
    int calculate(int[]... vars) {
        int sum = 0, b, c;
        for (b = 0; b < vars.length; b++) {
            for (c = 0; c < vars[b].length; c++) {
                sum += vars[b][c];
            }
        }
        return (sum);
    }
    public static void main(String args[]) {
        HelloWorld values = new HelloWorld();
        int sum = 0;
        sum = values.calculate(new int[]{10, 20, 30, 40}, new int[]{40, 50, 60});
        System.out.println("The sum of the numbers is:" + sum);
    }
}

A. The sum of the numbers is:25
B. The sum of the numbers is:100
C. The sum of the numbers is:250
D. Compilation Error due to invalid declaration of integer variable as arguments.
E. Compilation Error or Runtime Error for some other reason.
Topic: Unknown

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 c...sum of all elemnts in 2d array...varbl args can use instead of arrays

Posted by Uday Kumar    2014-07-10 08:51:10


ans is c sum of all elements in array by slicing accrdin to row and sum of all colum in row then it add to next row's colum sum

Posted by Dinesh Saini    2014-07-10 11:04:21


ANS C : var-args stores single int passed to the method in array...to pass group of ints in single method call at each argument its valid to use int[] declaration int[0] cause reference to first int[] object passed and int[1] carries refernce to second int[] object, up on executing 2nd for loop elements are invoked and summed to get result 250.

Posted by BalRam At    2014-07-10 15:42:27


Congratulations Uday Kumar. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-07-11 04:18:58


© meritcampus 2019

All Rights Reserved.

Open In App