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

    public class arr_output
    {
    public static void main(String args[])
    {
    int arr_var [] = new int[20];
    for (int j = 0; j < 20; ++j)
    {
    arr_var[j] = j;
    System.out.print(arr_var[j] + " ");
    j++;
    }
    }
    }
    1. 0 2 4 6 8 10 12 14 16 18
    2. 2 4 6 8 10 12 14 16 18
    3. 2 4 6 8 10
    4. 0 2 4 6 8 10
    5. 0 1 2 3 4 5 6 7 8 9 10
Correct Option: A

When an array is declared using new operator then all of its elements are initialized to 0 automatically. for loop body is executed 10 times as whenever controls comes in the loop i value is incremented twice, first by i++ in body of loop then by ++i in increment condition of for loop.
output: 0 2 4 6 8 10 12 14 16 18



Your comments will be displayed only after manual approval.