Menu
Topics Index
...
`

Method Overloading Arithmetic Utils


If you need explanation Read this topic

If you need Answer Take test on this topic

Method Overloading Arithmetic Utils
What will be the output of the following program?
class TestArithmeticUtils
{
    public static void main(String s[])
    {
        ArithmeticUtils utils = new ArithmeticUtils();
        int x = utils.add(2, 8);    // LINE M
        double y = utils.add(3.4, 6.6);    // LINE N
        double z = utils.add(x, y, 20);

        System.out.println("z = " + z);
    }
}

class ArithmeticUtils
{
    int add(int i, int j) // LINE A
    {
        return i + j;
    }

    double add(double i, double j) // LINE B
    {
        return i + j;
    }

    double add(int i, double j, int k)
    {
        return i + j + k;
    }

    double add(int i, double j, int k, double l) // LINE C
    {
        return i + j + k + l;
    }
}

A. Compilation Error - since the add methods at LINE A and LINE B have the same method signature.
B. Compilation Error - since it does not know which method to call at LINE M and LINE N
C. Compilation Error - since the method defined at LINE C is not used any where.
D. z = 40.0
Topic: Method Overloading In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is Option D.... Execution starts from main() method after execution of first stmt it will create object of Class ArithmeticUtils and then we are declaring variable "x" and we are initializing the value of x by calling the method add(2,8) of Class ArithmeticUtils... Inside Calss ArithmeticUtils we have overloaded methods i.e with the same method name this case add(2,8) we are passing integers as parameters so it will call int add(int i,int j) method and inside this we have return statement i+j the value is 10 so x value initialized with the value 10 x=10... after that we are creating double variable "y" and we are initializing this value by calling add(3.4,6.6) method in the class ArithmeticUtils here we have parameters are double value so double add(double i, double j) will execute this time so it will return i+j value as 10.0 and this y value is stored as 10.0 i.e y=10.0... after that we are creating a variable z and this value is initialized with by calling method add(x,y,20) inside the class and it is add(10,10.0,20) so it will call double add(int i,double j,int k) so it will return 10+10.0+20 as 40.0 bcz its return type is double then variable z will initialized with 40.0 after that println stmt will execute so it will print z=40.0.... if we have overloaded methods in a class then it will search for perticular parametric  method..... so ans is Option D

Posted by Bhagi Bhagyasri    2015-01-21 12:54:18


Ans : D
ArithmeticUtils class have 4 functions
Add(): 1. it takes to int values and perfom addtion and return int.
2. it takes double values and perform addtion and returns double to the calling function.
3. it takes 2 int values and 1 double values and perform addtion and returns double.
4 It takes 2 integer values and 2 double values and performs addition and returns double.

in main function of TestArithmeticUtils
creates object for ArithmeticUtils i.e util.
By object util it calls add(2,8)
the add() adds and return 10 it stores in x.

y=add(3.4,6.6) it adds  and retun 10.0.

z=add(x,y,20) ie it passing int double int i.e (10,10.0,20)
the function adds the values and returns 40.0
so now value in z is 40.0

Posted by Ananthoju Arun Chary    2015-01-21 14:01:51


Ans is D..here we have the method overloading concept...same method name with different type of order of num of arguments...in method overloading return type has no significance..

Execution:
in the AirthmeticUtils class we have add() method with different forms..
in the main() we are creating the instance for the AirthmeticUtils to access the methods in the AirthmeticUtils  class
we are calling the add() by passing the 2 int values so add() with 2 int args will get invoked than it will return the sum of arguments will stored in x value(10)
next we are calling the add() by passing the 2 decimal values..so add() with 2 double arguments get invoked(in java by default decimal value is treated as double) so here it will return sum(3.4+6.6=10.0)
next we are calling the double by passing the int,double,int than will invoke the 3rd method it will return the sum as 40 and we are printing 40.0

Posted by Uday Kumar    2015-01-21 15:40:23


This dose is now closed and the winners are Bhagi Bhagyasri,  for 'First Correct Comment', Ananthoju Arun Chary, Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Puneeth Shivalingaiah. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-01-22 08:28:00


© meritcampus 2019

All Rights Reserved.

Open In App