Menu
Question Index
...

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;
    }
}


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

Doubts

Problems

Topic: Method Overloading In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App