Q:

Java find output programs (Overloading) | set 1

belongs to collection: Java find output programs

0

Find the output of Java programs | Overloading | Set 1: Enhance the knowledge of Java Overloading concepts by solving and finding the output of some Java programs.

Question 1:

public class ClsOver {
  static void addValues(int val1, int val2) {
    int res = 0;

    res = val1 + val1;

    System.out.println("Sum of integers: " + res);
  }

  static void addValues(float val1, float val2) {
    float res = 0;

    res = val1 + val2;

    System.out.println("Sum of floats: " + res);
  }
  public static void main(String[] args) {
    addValues(20, 30);
    addValues(20.6, 30.4);
  }
}

Question 2:

public class ClsOver {
  void addValues(int val1, int val2) {
    int res = 0;

    res = val1 + val1;

    System.out.println("Sum of integers: " + res);
  }

  void addValues(float val1, float val2) {
    float res = 0;

    res = val1 + val2;

    System.out.println("Sum of floats: " + res);
  }
  public static void main(String[] args) {
    addValues(20, 30);
    addValues(20.6, 30.4);
  }
}

Question 3:

public class ClsOver {
  void addValues(int val1, int val2) {
    int res = 0;

    res = val1 + val1;

    System.out.println("###Sum of integers: " + res);
  }

  int addValues(int val1, int val2) {
    int res = 0;

    res = val1 + val1;

    System.out.println("@@@Sum of integers: " + res);
  }

  public static void main(String[] args) {
    ClsOver Ob = new ClsOver();

    Ob.addValues(20, 30);
  }
}

Question 4:

public class ClsOver {
  void addValues(int val1, int val2) {
    int res = 0;

    res = val1 + val2;

    System.out.println("###Sum of integers: " + res);
  }

  int addValues(int val1, int val2, int val3) {
    int res = 0;

    res = val1 + val2 + val3;

    System.out.println("@@@Sum of integers: " + res);
  }

  public static void main(String[] args) {
    ClsOver Ob = new ClsOver();

    Ob.addValues(20, 30);
    Ob.addValues(20, 30, 40);
  }
}

Question 5:

public class ClsOver {
  void addValues(int val1, int val2) {
    int res = 0;

    res = val1 + val2;

    System.out.println("Sum of integers: " + res);
  }

  int addValues(int val1, int val2, int val3) {
    int res = 0;

    res = val1 + val2 + val3;

    return res;
  }

  public static void main(String[] args) {
    ClsOver Ob = new ClsOver();

    int result = 0;

    Ob.addValues(20, 30);

    result = Ob.addValues(20, 30, 40);

    System.out.println("Result : " + result);
  }
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Question 1:

Output:

/ClsOver.java:19: error: no suitable method found for addValues(double,double)
    addValues(20.6, 30.4);
    ^
    method ClsOver.addValues(int,int) is not applicable
      (argument mismatch; possible lossy conversion from double to int)
    method ClsOver.addValues(float,float) is not applicable
      (argument mismatch; possible lossy conversion from double to float)
1 error

Explanation:

The above program will generate syntax error because of the below method call,

addValues(20.6,30.4);

In the above method call, we passed arguments of double types but we did not define any method with double arguments, we defined the method with float arguments. Here, we need to add suffix 'F' for float arguments.

The correct method call is given below:

addValues(20.6F,30.4F);

Answer Question 2:

Output:

/ClsOver.java:18: error: non-static method addValues(int,int) 
cannot be referenced from a static context
    addValues(20, 30);
    ^
/ClsOver.java:19: error: no suitable method found for addValues(double,double)
    addValues(20.6, 30.4);
    ^
    method ClsOver.addValues(int,int) is not applicable
      (argument mismatch; possible lossy conversion from double to int)
    method ClsOver.addValues(float,float) is not applicable
      (argument mismatch; possible lossy conversion from double to float)
2 errors

Explanation:

The above program will generate compile-time errors because we called non-static methods addValues() without creating an object in the static method main(). We cannot call non-static methods inside the static method of the class.

Answer Question 3:

Output:

/ClsOver.java:10: error: method addValues(int,int) is 
already defined in class ClsOver
  int addValues(int val1, int val2) {
      ^
1 error

Explanation:

The above program will generate a syntax error. In the above program, we created two AddValues() methods with the same name and same list of arguments but the return type is different. We cannot overload a method only based on the return type.

Answer Question 4:

Output:

/ClsOver.java:16: error: missing return statement
  }
  ^
1 error

Explanation:

The above program will generate syntax error because of the below method,

int addValues(int val1, int val2,int val3)
{
    int res = 0;

    res = val1 + val2 + val3;

    System.out.println("@@@Sum of integers: " + res);
}

The above method should return an integer value but we did not return any value from the above method that's why error gets generated by the above program.

Answer Question 5:

Output:

Sum of integers: 50
Result : 90

Explanation:

In the above program, we created a class ClsOver, here we overloaded the addValues() method. The first overloaded method took 2 arguments then add both argument values and print them on the console screen. Another overloaded method took 3 arguments and return the addition of all arguments.

Now look to the main() method, here we created object Ob of class ClsOver and the called both overloaded method and print the result on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java find output programs (Overloading) | set 2... >>
<< Java find output programs (Interface) | set 3...