Java OCA OCP Practice Question 541

Question

Given the following two methods, which method call will not compile?

public void myMethod(String... names) { 
        System.out.println(Arrays.toString(names)); 
} 
public void myMethods(String[] names) { 
        System.out.println(Arrays.toString(names)); 
} 
A.  myMethod("A"); 
B.  myMethod(new String[] { "B" }); 
C.  myMethods("C"); 
D.  myMethods(new String[] { "D" }); 


C.

Note

From within a method, an array or varargs parameter is treated the same.

However, there is a difference from the caller's point of view.

A varargs parameter can receive either an array or individual values, making Options A and B compile.

An array parameter can only take an array, which prevents Option C from compiling.




PreviousNext

Related