Menu
Topics Index
...
`

Method Overloading Output


If you need explanation Read this topic

If you need Answer Take test on this topic

Method Overloading Output
What will be the output of the following program?
class MethodOverloading
{
    public static void main(String[] args)
    {
        int a = 12;
        double b = 13;
        double c = m(a, b);
        double d = m(c, a);
        double e = m(a, (int) d);
        System.out.println("c = " + c + " d = " + d + " e = " + e);
    }

    public static double m(int x, double y)
    {
        return x + y;
    }

    public static double m(double x, double y)
    {
        return x - y;
    }

    public static double m(int x, int y)
    {
        return x % y;
    }
}

A. c = 25.0 d = 13.0 e = 12.0
B. c = 25.0 d = 13.0 e = 25.0
C. c = -1.0 d = -13.0 e = 25.0
D. Compilation Error
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.

A

Posted by Goutham Singarapu    2015-02-03 15:42:26


ANS IS A

--Here,the concept of "METHOD OVERLOADING" is used.

--METHOD OVERLOADING:It meand defining method with same name inside same class but with different parameters.

--for c=m(a,b)
1st method is called i.e. x+y---> 12+13=25 (double of it is
returned)-->25.0

----for d=m(c,a)
2nd method is called i.e. x-y---> 25.0+12.0=13.0

--for e=m(a,(int)d)
3rd method is called i.e. x%y---> 12.0+13.0=12.0

--c=25.0 d=13.0 e=12.0

Posted by Mânïshå Mùlchåndânï    2015-02-03 18:45:01


ans a. We can define more methods with the same name add as long as the parameters are different. But having a method with same parameters and different return type is not allowed. Similarly, the parameter names are of no importance and having two methods with same parameter type and order, but with different parameter names are not allowed. coming to this program initially int a is assigned with 12. double b assigned with 13.thn double c assigned with m( a,b) func cal.so here a is 12 b is 13 ,it overload parameters with int and double order,so a+b value get returned and stored in c.c=12+13.0=25.0. next d assigned with m( c,a) here c double, a int it overloads double, double method. int value implicitly converted into double. so 25.0-12.0=13.0, nxt e assigned with m(a,(int)d) here d value explicitly converted into int,but method returns double 12%13=12,returns 12.0,finally sop prints c=25.0,d=13.0, e =12.0

Posted by Maheshwari Natarajan    2015-02-03 18:58:13


This dose is now closed and the winners are Mânïshå Mùlchåndânï,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Maheshwari Natarajan for the 'Popular Comment'. The 'lucky liker' is Mânïshå Mùlchåndânï. 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-02-04 04:41:57


© meritcampus 2019

All Rights Reserved.

Open In App