Menu
Topics Index
...
`

Method Overloading Arithmetic Utils238


If you need explanation Read this topic

If you need Answer Take test on this topic

Method Overloading Arithmetic Utils238
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
        int z = utils.add(x, y, 20); // LINE O

        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 at LINE O.
B. Compilation Error at LINE M.
C. Compilation Error at LINE C.
D. z = 40
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 A..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.0 bcz its return type is double and in calling side we are taking into int variable so here compiler will raise an error sayinf loss of  precision

Posted by Uday Kumar    2015-01-22 05:12:30


Ans is Option A.... Execution Starts from main() method after execution of first stmt it will create object of Class ArithmeticUtils after that we are creating a variable "x" and we are initializing this variable with calling method add(2,8) inside the Class ArithmeticUtils.... and we have overloaded methods are there inside the class this add() method will call int add(int i, int j) bcz we are passing integer values as parameters so it execute that method and inside this we have one return stmt i+j it will return 10 to the variable x so it will initialized x =10 after that we are creating a variable "y" and initializing this variable with calling method add(3.4,6.6) and it will call method double add(double i,double j) inside this we have return stmt with i+j so the variable y is initialized with 10.0 and after that                                                                               here we will get error bcz we are creating int variable z and we are initializing this variable by calling method add(x,y,20) this will call the method double add(int i, double j,int k) here return type is double but we are storing double value into int variable we cannot convert double value into integer variable so here compilation error will occur..... so compilation occur at Line O so Ans is Option A

Posted by Bhagi Bhagyasri    2015-01-22 05:18:39


ans a.

Posted by Gaurav Bansal    2015-01-22 08:20:57


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar, Bhagi Bhagyasri,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is ?????????? ?????. 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-23 04:48:59


© meritcampus 2019

All Rights Reserved.

Open In App