Java OCA OCP Practice Question 1662

Question

What would be the result of compiling and running the following program?.

// Filename: Main.java
class Main {//from   w w w .  ja  v  a 2 s . c om
  public static void main(String[] args) {
    int size = 20;
    int[] arr = new int[ size ];

    for (int i = 0; i < size; ++i) {
      System.out.println(arr[i]);
    }
  }
}

Select the one correct answer.

  • (a) The code will not compile, because the array type int[] is incorrect.
  • (b) The program will compile, but will throw an ArrayIndexOutOfBoundsException when run.
  • (c) The program will compile and run without error, but will produce no output.
  • (d) The program will compile and run without error, and will print the numbers 0 through 19.
  • (e) The program will compile and run without error, and will print 0 twenty times.
  • (f) The program will compile and run without error, and will print null twenty times.


(e)

Note

The array declaration is valid, and will declare and initialize an array of length 20 containing int values.

All the values of the array are initialized to their default value of 0.

The for(;;) loop will print all the values in the array, that is, it will print 0 twenty times.




PreviousNext

Related