Java OCA OCP Practice Question 1489

Question

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

public class Main {
   public static void main(String args[]) {
      int size = 10;
      int[] arr = new int[size];
      for (int i = 0; i < size; ++i)
         System.out.println(arr[i]);
   }
}

Select 1 option

  • A. The code will fail to compile, because the int [] array declaration is incorrect.
  • B. The program will compile, but will throw an IndexOutOfBoundsException when run.
  • C. The program will compile and run without error, and will print nothing.
  • D. The program will compile and run without error and will print null ten times.
  • E. The program will compile and run without error and will print 0 ten times.


Correct Option is  : E

Note

Elements of Arrays of primitive types are initialized to their default value.

Elements of Arrays of non-primitive types are initialized to null.




PreviousNext

Related