Question 19
Back to Exercise
What is the output of the following Java program?
class Team {
final String name;
final String[] players;
Team(String name, String... players) { this.name = name; this.players = players; }
}
public class Main {
public static void main(String[] args) {
Team a = new Team("A"), b = new Team("B", "Asha"), c = new Team("C", "Ravi", "Meena", "John");
System.out.println(a.players.length + " " + b.players.length + " " + c.players.length + " " + c.players[2]);
}
}
- A 0 1 3 John Correct
- B 1 1 3 Meena
- C 0 1 3 Meena
- D NullPointerException
Correct Answer: Option A
With no extra arguments the varargs parameter is an empty array, not null.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!