Methods - Overloading & Varargs QuizJ8 Home « Methods - Overloading & Varargs Quiz

Java Objects & Classes Quiz 7

The quiz below tests your knowledge of the material learnt in Objects & Classes - Lesson 7 - Methods - Overloading & Varargs.

Question 1 : We can have more than one method with the same name in a class?
- We can have more than one method with the same name in a class and this is known as <i>overloading</i>.
Question 2 : Is the following method declaration valid?
static void addNumbers(int ... number, String string) {
    System.out.println("Total = " + string);
}
- When using <i>varargs</i> they must come last in the parameter list and in the code snippet <i>varargs</i> was first.
Question 3 : What do we have to be careful of when overloading methods that use varargs
- We have to be careful of ambiguity when overloading methods that use <i>varargs</i>, the compiler needs to resolve parameter lists to one method.
Question 4 : Is the following method declaration valid?
static void addNumbers(int ... number, String ... string) {
    System.out.println("Total = ");
}
- When using <i>varargs</i> only one is allowed in the parameter list and the code snippet has two.
Question 5 : What is overloading?
- Using the same name for a method in a class more than once is known as <i>overloading</i>. Using the same name for a method in a subclass as a superclass is known as <i>overriding</i>.
Question 6 : Is the following method declaration valid?
static void addNumbers(int number, String ... string) {
    System.out.println("Total = ");
}
- The code snippet has only one <i>vararg</i> which comes last in the parameter list so is valid.
Question 7 : When using overloading each method must have different parameter types so the compiler knows which method to call?
- When using <i>overloading</i> it is ok to have the same parameter types, so long as there order differs, so the compiler knows which method to call.
Quiz Progress Bar Please select an answer


What's Next?

The next quiz we test your knowledge of instance variables, how we use them and their scope.