LearnFrenzy
Sign in Take a free test

Constructors and the this Keyword · Predict the Output · Question 19

A varargs constructor Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 19 Back to Exercise

What is the output of the following Java program?

Main.java
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links