Study and learn Interview MCQ Questions and Answers on Varargs or Variable Arguments in Java. Attend job interviews easily with these Multiple Choice Questions. You can print these Questions in default mode to conduct exams directly. You can download these MCQs in PDF format by Choosing Print Option first and Save as PDF option next using any Web Browser.
Go through Java Theory Notes on Varargs before reading these objective questions.
Only constructors and methods accept arguments. So, Java Varargs are applicable only to these.
A Java Vararg represents simply a variable number of arguments.
A Java-Vararg represents a variable of a particular type.
Yes. A Vararg can be a non-primitive or object type also along with a primitive type like byte, short, int, long, float, double etc.
A Vararg can come only as the last argument in the list of arguments of a constructor or method.
public class Varargs1 { static void displayStudents(String... stu) { for(String s: stu) System.out.print(s + " "); } public static void main(String args[]) { displayStudents("Bean", "Atkinson", "Milton"); } }
In the above example, the Vararg variable "stu" is of String array type. So, we have used a FOR loop to go through all the elements. We also use a NORMAL FOR loop with an index starting from 0.
public class Varargs2 { void attendance(String... allStu) { System.out.println("Attended: " + allStu.length); } void attendance(boolean... all) { System.out.println("Attended: " + all.length); } public static void main(String args[]) { new Varargs2().attendance(); } }
Attended: 0
Attended: 0 Attended: 0
Observe that there is no default constructor with 0 arguments. When no argument is passed, the compiler can not choose which attendance() method to choose. So it gives an error.
The compiler simply gives an error when two methods look the same to the compiler for calls at compile time.
public class Varargs3 { Varargs3(int... dates) { System.out.println("Inside Varargs(int...)"); } Varargs3(boolean... yesno) { System.out.println("Inside Varargs(float...)"); } public static void main(String[] args) { new Varargs3(); } }
Inside Varargs(int...)
Inside Varargs(boolean...)
Inside Varargs(int...) Inside Varargs(boolean...)
As the two constructors have the same method signature with zero arguments, the compiler sees those as the same. So, it throws an error saying the Constructor is ambiguous.
public class Varargs4 { Varargs4(int... carnums) { System.out.println("Inside Varargs(int...)"); } Varargs4(float... prices) { System.out.println("Inside Varargs(float...)"); } public static void main(String[] args) { new Varargs4(); } }
Inside Varargs(int...)
Inside Varargs(int...) Inside Varargs(float...)
In the above example, both constructors are overloading one another. In such cases, the compiler chooses the Constructor or Method with a lower sized data type as an argument.
public class Varargs5 { Varargs5(int...weights, boolean yesno) { System.out.println("AMAZON"); } public static void main(String[] args) { //new Varargs5(20, true); } }
Yes. The compiler throws errors "Unresolved Compilation Problem" and "The variable argument type of the method or constructor must be the last parameter"
The data type (primitive or object) immediately followed by three dots and a variable name separated by a space create a Variable Argument.
Yes, only one. Because a VARARG can be present only as the last parameter or argument.
Yes. There is no limit. There can be any number of methods or constructors with one Vararg per each method or constructor.