Multiple choice technology programming languages

A programmer needs to create a logging method that can accept an arbitrary number of arguments. For example, it may be called in these ways: logIt(”log message 1 “); logIt(”log message2”,”log message3”); logIt(”log message4”, “log message5”, “log message6); Which declaration satisfies this requirement?

  1. public void logIt(String * msgs)

  2. public void logIt(String [] msgs)

  3. public void logIt(String... msgs)

  4. public void logIt(String msg1, String msg2, String msg3)

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

Java's varargs feature uses the ... syntax to accept a variable number of arguments. The declaration String... msgs allows zero or more String arguments to be passed, while still being compatible with passing an explicit String array.

AI explanation

Varargs syntax (String... msgs) lets a method accept zero or more arguments of the given type, which the compiler packages into an array inside the method — exactly what's needed to support calls with one, two, or three String arguments through a single method signature. A fixed parameter list like (String msg1, String msg2, String msg3) would only match calls with exactly three arguments, and String[] or String* aren't valid ways to express a variable-arity parameter in a method call site.