Test Series - java

Test Number 58/64

Q: Java Varargs are applicable only for ___.
A. Constructors
B. Methods
C. Both Constructors and Methods
D. None
Solution: Only constructors and methods accept arguments. So, Java Varargs are applicable only to these.
Q: A Java-Vararg is nothing but ____.
A. Variable number of arguments
B. Variable type of arguments
C. Variable size of arguments
D. All
Solution: A Java Vararg represents simply a variable number of arguments.
Q: A Java vararg is a ____.
A. Method
B. Constructor
C. Variable
D. All
Solution: A Java-Vararg represents a variable of a particular type.
Q: A Java-Vararg can be of any type like primitive or object type. State TRUE or FALSE.
A. TRUE
B. FALSE
C. None
D. Compiler error
Solution: 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.
Q: A Java Vararg or Variable Argument can come at any position in a method or constructor. State TRUE or FALSE.
A.  FALSE
B. TRUE
C. None
D. Compiler error
Solution: A Vararg can come only as the last argument in the list of arguments of a constructor or method.
Q: What is the output of the below java program with varargs?
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");
  }
}
A. Bean Bean Bean
B. null null null
C. Bean Atkinson Milton
D. Compiler error
Solution: 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.
Q: What is the output of the below Java program with Variable arguments?
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();
  }
}
A. Attended: 0
B. Attended: 0 Attended: 0
C. No Output
D. Compiler Error
Solution: 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.
Q: Which is the error thrown when two methods with varargs look the same to the compiler?
A. The method is ambiguous
B. The method is difficult to choose
C. The method signature is not correct
D. None
Solution: The compiler simply gives an error when two methods look the same to the compiler for calls at compile time.
Q: What is the output of the below Java program with Varargs?
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();
  }
}
A. Inside Varargs(int...)
B. Inside Varargs(boolean...)
C. Inside Varargs(int...) Inside Varargs(boolean...)
D. Compiler error
Solution: 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.
Q: What is the output of the below Java program with Varargs?
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();
  }
}
A. Inside Varargs(int...)
B. Inside Varargs(int...) Inside Varargs(float...)
C. No output
D. Compiler error
Solution: 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.

You Have Score    /10