Java OCA OCP Practice Question 878

Question

What happens when calling the following method with a non-null and non-empty array?

public static void myMethod(String[] names) { 
        names[names.length] = "Times Square"; 
} 
  • A. It adds an element to the array the value of which is Times Square.
  • B. It replaces the last element in the array with the value Times Square.
  • C. It does not compile.
  • D. It throws an exception.


D.

Note

names.length is the number of elements in the array.

The last valid index in the array is one less than names.length.

In Java, arrays do not resize automatically.

The code throws an ArrayIndexOutOfBoundsException.




PreviousNext

Related