Skip to the content

onlineexamguide

  • Home
  • Courses
  • Engineering Study Materials
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login
  • Home
  • Courses
  • Engineering Study Materials
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login

VarArgs in java or Variable Arguments Tutorial with Examples

VarArgs in java

Table of Contents

  • VarArgs in java or Variable Arguments Explained
    • Java Varargs in a Constructor
    • Java Varargs in a Method
    • VarArgs in JAVA Interview MCQ
    • 1) Java Varargs are applicable only for ___.
    • 2) A Java-Vararg is nothing but ____.
    • 3) A Java vararg is a ____.
    • 4) A Java-Vararg can be of any type like pri-mitive or object type. State TRUE or FALSE.
    • 5) A Java Vararg or Variable Argument can come at any position in a method or constructor. State TRUE or FALSE.
    • 8) Which is the error thrown when two methods with varargs look the same to the compiler?
    • 12) Which is the operator used to represent a Vararg type in a method or constructor in Java?
    • 14) What is the maximum number of methods or constructors with Varargs in a single Java class?

Study and learn Interview MCQ Questions and Answers on VarArgs in java or Variable Arguments in Java. Attend job interviews easily with these Multiple Choice Questions

Java Varargs refer to Variable Arguments. Java Varargs are applicable only for Constructors and Methods. Let us know more in Java Tutorial.

VarArgs in java or Variable Arguments Explained

Java Varargs allow writing constructors or methods that accept any unknown (arbitrary) number of arguments. The number of arguments may be zero also. These Java Variable Arguments successfully implement Method Overloading or Constructor Overloading by maintaining unique method signatures.

You can make any Variable of Pri-mitive or Object type, a Variable Arguments (Varargs) type. To do this, you need to add Three Dots (…) next to the Data-Type within the Parameters List of a method or constructor.

A Java Vararg variable is treated as an Array. A Vararg variable can come only in the last position in a parameter list. In a single Method or Constructor, only one Varargs type variable defined.

Java Varargs in a Constructor

The syntax is as follows.

CONSTRUCTOR_NAME(TYPE... VARIABLE_NAME)
{
  //CODE for initialization
}

Check the below example that implements Varargs inside a Constructor’s Parameter List.

public class VarargsExample
{
  VarargsExample(int... a)
  {
    if(a.length == 0)
      System.out.println("Zero Arguments");
    for(int i=0; i<a.length; i++)
      System.out.print(a[i] + ",");
    System.out.println();
  }

  VarargsExample(String name, int...a)
  {
    System.out.println("Name= " + name);
    for(int i=0; i<a.length; i++)
      System.out.print(a[i] + ",");
  }

  public static void main(String[] args)
  {
    VarargsExample obj = new VarargsExample();
    VarargsExample ve = new VarargsExample(5,6,7);
    VarargsExample ve2 = new VarargsExample("CANADA", 1,2);
  }
}
//OUTPUT
Zero Arguments

5,6,7,
Name= CANADA
1,2,

Notice that we have not maintained a Zero-Argument constructor in the above example. Even then, the constructor with int…a arguments is invoked for new VarargsExample() call. If you put a constructor with zero arguments, it will be invoked instead of Variable Arguments type constructor if it is the first constructor matched for zero arguments.

Let us check another example that shows incompatible constructor overloading using Varargs.

public class VarargsExample
{
  VarargsExample(boolean...a)
  {

  }
  VarargsExample(int... a)
  {

  }
  public static void main(String[] args)
  {
    VarargsExample ve = new VarargsExample(); //Error
  }

}
//OUTPUT
Compiler error

The above example produces an error “The Constructor is Ambiguous“. Because int and boolean are incompatible or inconvertible data types. If we replace boolean with a number data type like float, double, long or byte, the program compiles and gives the output.

Note: While choosing a Vararg constructor, the compiler selects the one with a small-sized data type first. So CONSTRUCTOR(byte…a) is selected before CONSTRUCTOR(int…a).

Java Varargs in a Method

Just like in a Constructor with Varargs, Java methods too can have Varargs. Only one Vararg type variable is allowed in a method that too at the end of the parameters list.

The syntax is as follows.

METHOD_NAME(TYPE... VARIABLE_NAME)
{
  //CODE for initialization
}

Check the below example that implements Varargs inside a Method’s Parameter List.

public class VarargsMethodExample
{
  void show(String...names)
  {

  }
  void show(int...codes)
  {

  }
  void modify(String operator, long...transmission)
  {

  }
  String report(float var, int...codes)
  {
    String a = "";
    for(int i=0; i<codes.length; i++)
    {
      a = a + codes[i] + "," ;
    }
    return a;
  }

  public static void main(String[] args)
  {
    VarargsMethodExample vme = new VarargsMethodExample();
    String rep = vme.report(0.0f, 2,3,4);
    System.out.println("Report= " + rep);
  }
}
//OUTPUT
Report= 2,3,4,

In the above example, we have used Java Varargs in the methods show(), modify() and report(). Method “show” is overloaded. Method “report” even returns a value.

[WpProQuiz 163]

VarArgs in JAVA Interview MCQ

1) Java Varargs are applicable only for ___.

A) Constructors

B) Methods

C) Both Constructors and Methods

D) None

Answer [=] C

Explanation:

Only constructors and methods accept arguments. So, Java Varargs are applicable only to these.

2) A Java-Vararg is nothing but ____.

A) Variable number of arguments

B) Variable type of arguments

C) Variable size of arguments

D) All

Answer [=] A

Explanation:

A Java Vararg represents simply a variable number of arguments.

3) A Java vararg is a ____.

A) Method

B) Constructor

C) Variable

D) All

Answer [=] C

Explanation:

A Java-Vararg represents a variable of a particular type.

4) A Java-Vararg can be of any type like pri-mitive or object type. State TRUE or FALSE.

A) TRUE

B) FALSE

Answer [=] A

Explanation:

Yes. A Vararg can be a non-primitive or object type also along with a pri-mitive type like byte, short, int, long, float, double etc.

5) A Java Vararg or Variable Argument can come at any position in a method or constructor. State TRUE or FALSE.

A) FALSE

B) TRUE

Answer [=] B

Explanation:

A Vararg can come only as the last argument in the list of arguments of a constructor or method.

6) 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

Answer [=] C

Explanation:

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.

7) 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

Answer [=] D

Explanation:

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.

8) 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

Answer [=] A

Explanation:

The compiler simply gives an error when two methods look the same to the compiler for calls at compile time.

9) 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

Answer [=] D

Explanation:

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.

10) 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

Answer [=] A

Explanation:

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.

11) What is the output of the below code snippet?

public class Varargs5
{
  Varargs5(int...weights, boolean yesno)
  {
    System.out.println("AMAZON");
  }
  public static void main(String[] args)
  {
   //new Varargs5(20, true);
  }
}

A) No output

B) Error: The variable argument type int of the method Varargs5 must be the last parameter

C) Error: Varargs do not allow other data types

D) None

Answer [=] B

Explanation:

Yes. The compiler throws errors “Unresolved Compilation Problem” and “The variable argument type of the method or constructor must be the last parameter”

12) Which is the operator used to represent a Vararg type in a method or constructor in Java?

A) One Dot (.)

B) Two Dots (..)

C) Three Dots (…)

D) DOT DOT DOT COMMA (…,)

Answer [=] C

Explanation:

The data type (pri-mitive or object) immediately followed by three dots and a variable name separated by a space create a Variable Argument.

13) How many maximum numbers of Varargs or Variable-Arguments can be there in a method or a constructor in Java?

A) 1

B) 2

C) 8

D) 16

Answer [=] A

Explanation:

Yes, only one. Because a VARARG can be present only as the last parameter or argument.

14) What is the maximum number of methods or constructors with Varargs in a single Java class?

A) 1

B) 2

C) 8

D) There is no limit

Answer [=] D

Explanation:

Yes. There is no limit. There can be any number of methods or constructors with one Vararg per each method or constructor.


For More Free online Quizzes of JAVA PROGRAMMING Test online Click here


For More Free online Test of previous years Question Papers of Aptitude Click here

Write a comment Cancel reply

You must be logged in to post a comment.

*
  • प्रवासन के लिए अंतर्राष्ट्रीय संगठन की पहली महिला महानिदेशक एमी पोप (Amy Pope) किस देश से हैं?

  • उत्तर – अमेरिका

  • किस केंद्रीय मंत्रालय ने ‘मेरी LiFE, मेरा स्वच्छ शहर’ अभियान शुरू किया?

  • उत्तर – आवास और शहरी मामलों के मंत्रालय

  • किस देश में अत्यधिक रोगजनक एवियन इन्फ्लुएंजा (Highly Pathogenic Avian Influenza – HPAI) की पुष्टि हुई है?

  • उत्तर – ब्राजील

  • किस संस्था ने ‘राष्ट्रीय ऊर्जा प्रबंधन केंद्र’ (National Energy Management Centre) की स्थापना की है?

  • उत्तर – REMC लिमिटेड

  • किस संस्था ने ‘World Tourism Barometer’ रिपोर्ट जारी की?

  • उत्तर – UNWTO

  • ‘साइबर सुरक्षित भारत’ किस संस्था/केंद्रीय मंत्रालय की पहल है?

  • उत्तर – इलेक्ट्रॉनिक्स और आईटी मंत्रालय

  • हाल ही में ख़बरों में रहा ‘प्रोजेक्ट-स्मार्ट’ किस केंद्रीय मंत्रालय से संबंधित है?

  • उत्तर – शहरी मामले मंत्रालय-रेलवे मंत्रालय

  • किस राज्य/केन्द्र शासित प्रदेश ने ‘जगन्नानकु चेबुदम कार्यक्रम’ (Jaganannaku Chebudam Programme) का शुभारंभ किया?

  • उत्तर – आंध्र प्रदेश

  • किस देश द्वारा प्रथम चीन-मध्य एशिया शिखर सम्मेलन (China-Central Asia Summit) की मेजबानी की जाएगी?

  • उत्तर – चीन

  • किस संस्था ने ‘Race to Net Zero’ शीर्षक से एक रिपोर्ट जारी की?

  • उत्तर – UN ESCAP

Recent Posts

  • CONSTRUCTION OF Cables & Selection
  • Ferranti Effect in transmission line
  • Insulator
  • String Efficiency
  • Corona Effect in Overhead Transmission Line
  • Types of Conductor
  • Proximity Effect
  • Skin Effect
  • What is GMD and GMR in Transmission Lines
  • Synchronous Reluctance Motor
  • Pass by Value and Pass by Reference in Java
  • JAVA Methods
  • BLDC Motor
  • OOPS Concepts in Java
  • Command line argument in JAVA

onlineexamguide

onlineexamguide.com is the ultimate guide that will keep you updated about almost every Exam & Interviews . We aim to provide our readers with an informative details that have been occurring in Examination . Here at onlineexamguide.com , we focus on delivering our readers with the latest exam Pattern Mock test

We Provide Free online test to practice for Competitive exams , Online Exam, Entrance and Interview. Learn and Practice online test for Free and Prepare for your exam online with us

Quick links

  • About us
  • Privacy Policy
  • Instructor Registration
  • Student Registration
  • Java Programming
  • C programming
  • C++ programming
  • Aptitude Tricks

Follow us

Free Online Mock Test

  • UPTET PRIMARY Online Test Series
  • Super TET Mock Test in Hindi 2023
  • CTET Mock Test 2022 Paper 1
  • SSC CHSL Online Mock Test
  • SSC MTS Mock Test 2023
  • SSC CGL Mock Test
  • SSC GD Mock Test
  • ccc online test

Search

Learn and Earn

Register as Instructor - Create and sell online courses and coaching services with the best online platform onlineexamguide.com . Build a course, build a brand, earn money

Contact us

For any queries

Email us on - admin@onlineexamguide.com

We will response fast as much as we can
Copyright © 2023 onlineexamguide.com - All Rights Reserved.
error: Content is protected !!

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.