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

    public class arr_var
    {
    public static void main(String args[])
    {
    int array_var [] = new int[20];
    for (int i = 0; i < 20; ++i) {
    array_var[i] = i/2;
    array_var[i]++;
    System.out.print(array_var[i] + " ");
    i++;
    }

    }
    }
    1. 1 2 3 4 5 6 7 8 9 10
    2. 1 2 3 4 5
    3. 0 1 2 3 4 5 6 7 8 9 10
    4. 1 2 3 4 5 6 7 8 9
    5. 0 1 2 3 4 5
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 5 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: 1 2 3 4 5 6 7 8 9 10



Your comments will be displayed only after manual approval.