Java OCA OCP Practice Question 526

Question

Fill in the blank in the following code to get the first element from the varargs parameter.

public void toss (MyClass... f) { 
        MyClass first =                ; 
} 
  • A. f
  • B. f[0]
  • C. f[1]
  • D. None of the above


B.

Note

Array indexes are zero based in Java.

A varargs parameter is simply another way of passing in data to a method.

From within the method, it is treated just like you had written MyClass[] f as the method parameter.

Therefore, the first element uses the 0th index, and Option B is correct.




PreviousNext

Related