Home » JAVA Programming » Arrays » Question
  1. What is the output of this program?

    public class multidimention_arr
    {
    public static void main(String args[])
    {
    int array[][] = new int[5][5];
    array[0] = new int[1];
    array[1] = new int[2];
    array[2] = new int[3];
    int sum = 0;
    for (int i = 0; i < 5; ++i)
    {
    for (int j = 0; j < i + 1; ++j)
    {
    array[i][j] = j + 1;
    }
    }
    for (int i = 0; i < 5; ++i)
    {
    for (int j = 0; j < i + 1; ++j)
    {
    sum = sum + array[i][j];
    }
    }
    System.out.print(sum);
    }
    }
    1. 25
    2. 10
    3. 35
    4. 20
    5. 30
Correct Option: C

arr[][] is a 2D array, array has been allotted memory in parts. 1st row contains 1 element, 2nd row contains 2 elements, 3rd row contains 3 elements, 4th row contains 4 elements, and 5th row contains 5 elements. each element of array is given i + j value in loop. sum contains addition of all the elements of the array.
output: 35



Your comments will be displayed only after manual approval.