Object Oriented Programming using Java Questions and Answers – Method Overloading

This set of Object Oriented Programming (OOPs) using Java Multiple Choice Questions & Answers (MCQs) focuses on “Method Overloading”.

1. What will be the output of the following Java code?

class Calc 
{ 
    public int disp() 
    { 
        return 10; 
    } 
    public long disp()
    { 
        return 11; 
    } 
    public static void main(String[] args) 
    { 
        Calc obj = new Calc(); 
        System.out.println(obj.disp());     
    } 
}

a) 10
b) 11
c) Compilation Error
d) Null
View Answer

Answer: c
Explanation: In the above program, there are two functions disp() with the same function signature and function prototype. Since there is no difference between the two function signatures, the compiler is not able to implement the concept of function or method overloading and thereby a compilation error is thrown.
Output:

$ javac Calc.java
$ java Calc 
Uncompilable source code

2. What will be the output of the following Java code?

advertisement
advertisement
class Calc1 
{ 
    public int disp() 
    { 
        return 10; 
    } 
    public long disp(int a)
    { 
        return 11; 
    } 
    public static void main(String[] args) 
    { 
        Calc obj = new Calc(); 
        System.out.println(obj.disp(11));     
    } 
}

a) 10
b) 11
c) Compilation Error
d) Null
View Answer

Answer: b
Explanation: The above block of code uses the concept of method overloading. Two or more methods can have same name inside the same class if they accept different arguments. In this case, as function disp() occurs twice with different parameters, the program is executed successfully and returns 11 as mentioned in the function body.
Output:

$ javac Calc1.java
$ java Calc1 
11

3. What will be the output of the following Java code?

advertisement
class Display 
{
    private static void display(int a)
    {
        System.out.println(a);
    }
    private static void display(int a, int b)
    {
        System.out.println(a +" "+ b);
    }
    public static void main(String[] args) 
    {
        display(5);
    }
}

a) 5
b) 4
c) Compilation Error
d) 1 4
View Answer

Answer: a
Explanation: When a class has multiple methods by same name but consists of different parameters, it is known as function overloading. In the above program, there are two display() functions which have different function signatures. As the function with two parameters in called in the main() method, the value 5 is printed.
Output:

advertisement
$ javac Display.java
$ java Display 
5

4. What will be the output of the following Java code?

class Float 
{
    static void display(int a, float b)
    {
        System.out.println(a +" "+ b);
    }
    public static void main(String[] args) 
    {
        display(1, 4);
    }
}

a) 1.0 4.0
b) 1 4.0
c) Compilation Error
d) 1 4
View Answer

Answer: b
Explanation: Overloaded methods are differentiated based on the number and type of the parameters passed as arguments to the function when method overloading is used in a program. In this case, the integer value 4 is converted to float value 4.0 automatically by the compiler as there is no loss of precision mathematically and the program is executed.
Output:

$ javac Float.java
$ java Float 
1 4.0

5. What will be the output of the following Java code?

class PrintValue
{
    private static void display(int a)
    {
        System.out.println("string");
    }
    private static void display(String a)
    {
        System.out.println("int");
    }
    public static void main(String[] args) 
    {
        display("Hello");
    }
}

a) string
b) int
c) Compilation Error
d) Hello
View Answer

Answer: b
Explanation: Method overloading or function overloading is a feature of object oriented programming where polymorphism is implemented. In the above program, there are two display() functions which have different function signatures but have the same number of parameters. Since a string is passed in the main() method, the method with string arguments is executed.
Output:

$ javac PrintValue.java
$ java PrintValue  
int

6. What will be the output of the following Java code?

class DataType
{
    static void print(int a)
    {
        System.out.println("string");
    }
    static void print(String a, int c)
    {
        System.out.println("int");
    }
    public static void main(String[] args) 
    {
        print("0");
    }
}

a) 0
b) int
c) string
d) Compilation error
View Answer

Answer: d
Explanation: If the argument lists of both the given functions are different in terms of number of parameters and their types, the polymorphism aspect of object oriented programming is invoked. This phenomenon is called method or function overloading. In this case, the main() method calls a single string value but there is no function print() with exatly one string value, hence an error is thrown.
Output:

$ javac DataType.java
$ java DataType 
Uncompilable source code

7. What will be the output of the following Java code?

class StringandInteger
{
    static void print(int a)
    {
        System.out.println("string");
    }
    static void print(String a, int c)
    {
        System.out.println("int");
    }
    public static void main(String[] args) 
    {
        print("0",5);
    }
}

a) 0
b) int
c) string
d) compilation error
View Answer

Answer: b
Explanation: Multiple methods can have same name inside the same class in a program if they accept different arguments in terms of their datatype or the number of arguments. This feature is known as method overloading. In this case, the main() method calls the second print() function with two arguments.
Output:

$ javac StringandInteger.java
$ java StringandInteger
int

8. What will be the output of the following Java code?

class StringandInteger2
{
    static void print(int a)
    {
        System.out.println("string");
    }
    static void print(String a, int c)
    {
        System.out.println("int");
    }
    public static void main(String[] args) 
    {
        print(150,"int");
    }
}

a) 0
b) int
c) string
d) compilation error
View Answer

Answer: d
Explanation: Method overloading is a concept in Java that allows to have many functions with same name but different parameters. The order of datatype also matters. In this case, an integer and string are passed, but even though the second print() function contains the same datatypes, they are not in the same order. Hence, an error is thrown.
Output:

$ javac StringandInteger2.java
$ java StringandInteger2
Uncompilable source code

9. What will be the output of the following Java code?

class Sum
{
    static void calc(int a)
    {
        System.out.println(a+a);
    }
    static void print(double a, int c)
    {
        System.out.println(a+c);
    }
    public static void main(String[] args) 
    {
        print(15.0,5);
    }
}

a) 15.5
b) 20.0
c) 20
d) Compilation Error
View Answer

Answer: b
Explanation: If the data types are compatible, then Java will perform the conversion automatically known as Automatic Type Conversion. In this case, since the main() method calls the second print() function, the integer is directly converted to double datatype by the compiler and does the arithmetic after that.
Output:

$ javac Sum.java
$ java Sum 
20.0

10. What will be the output of the following Java code?

class Sum2
{
    static void print(int a)
    {
        System.out.println(a+a);
    }
    static void print(float a, int c)
    {
        System.out.println(a+c);
    }
    public static void main(String[] args) 
    {
        print(15.0,5);
    }
}

a) 15.5
b) 20.0
c) 20
d) Compilation Error
View Answer

Answer: d
Explanation: In the above program, the concept of function or method overloading is used. As there are two parameters used to call the function the main() method, the second function apparently seems to be executed but that’s not the case because the value of 15.0 is read as a double datatype and not float. Hence an error is thrown by the compiler.
Output:

$ javac DataType.java
$ java DataType 
Uncompilable source code

11. What will be the output of the following Java code?

class FloatAdd
{
    static void print(float a, double b)
    {
        System.out.println(a+a);
    }
    static void print(float a, float c)
    {
        System.out.println(a+c);
    }
    public static void main(String[] args) 
    {
        print(15.0,5.0);
    }
}

a) 15.5
b) 20.0
c) 20
d) Compilation Error
View Answer

Answer: d
Explanation: Method overloading or function overloading is a feature of object oriented programming where polymorphism is implemented. In the above program, there are two print() functions and none of them are executed due to mismatch of datatypes in the main() method.
Output:

$ javac FloatAdd.java
$ java FloatAdd
Uncompilable source code

12. What will be the output of the following Java code?

class FloatAdd2
{
    static void calc(float a, double b)
    {
        System.out.println(a+a);
    }
    static void calc(float a,float c)
    {
        System.out.println(a+c);
    }
    public static void main(String[] args) 
    {
        calc(15.0f,5.0);
    }
}

a) 30.0
b) 20.0
c) 20
d) Compilation Error
View Answer

Answer: a
Explanation: Multiple methods can have same name inside the same class in a program if they accept different arguments in terms of their datatype or the number of arguments. This feature is known as method overloading. The function calc() in this case is called in the main() method and is successfully executed.
Output:

$ javac FloatAdd2.java
$ java FloatAdd2
30.0

13. What will be the output of the following Java code?

class PointAdd
{
    static void calc(float a, double b)
    {
        System.out.println(a+a);
    }
    static void calc(float a,int c)
    {
        System.out.println(a+c);
    }
    public static void main(String[] args) 
    {
        calc(15.0f, 10);
    }
}

a) 30.0
b) 20.0
c) 25.0
d) Compilation Error
View Answer

Answer: c
Explanation: Method overloading is a concept in Java that allows to have many functions with same name but different parameters. The order of datatype also matters. Here, the float and integer values, 15.0f and 10 are passed to the calc() function. Hence, 25.0 is attained as the output.
Output:

$ javac PointAdd.java
$ java PointAdd
25.0

14. What will be the output of the following Java code?

class Calculations
{
    static float calc(float a, double b)
    {
        return(a+a);
    }
    static float calc(float a,int c)
    {
        return(a+c);
    }
    public static void main(String[] args) 
    {
        System.out.println(calc(15.0f,1));
    }
}

a) 30.0
b) 20.0
c) 16.0
d) Compilation Error
View Answer

Answer: c
Explanation: Method overloading is a feature of object oriented programming which allows multiple functions to have the same number but should be distinguishable by their signature and prototype. The two calc() funtions have different datatypes as parameters, hence the second function is called in the main() method and is executed.
Output:

$ javac Calculations.java
$ java Calculations
16.0

15. What will be the output of the following Java code?

class Calculation2
{
    static float calc(float a, double b)
    {
        return(a*b);
    }
    static int calc(int a,int c)
    {
        return(a+c); 
    }
    public static void main(String[] args) 
    {
        System.out.println(calc(15,0));
    }
}

a) 15
b) 0
c) 16
d) Compilation Error
View Answer

Answer: a
Explanation: When a class consists of multiple methods by same name but having different parameters, it is known as method overloading. Since 15 and 0 are integer datatypes, the compiler executes the second calc() function and prints their arithmetic sum.
Output:

$ javac Calculation2.java
$ java Calculation2
15

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming (OOPs) using Java, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.