What is the output when the following code runs Explain whic
What is the output when the following code runs? Explain which methods are called, in which order, what the various parameters being passed in are, and what values are returned.
class Num {
//Add method 1
void add(int a) {
System.out.println(\"a: \" + a);
}
//Add method 2
int add (int a, int b) {
System.out.println(\"a and b: \" + a + \",\" + b);
return a+b;
}
//Add method 3
double add(double a) {
System.out.println(\"double a: \" + a);
return a+a;
}
public static void main(String args []) {
Num num = new Num();
double result;
num.add(10);
int sum = num.add(15, 25);
System.out.println(sum);
result = num.add(7.7);
System.out.println(\"Result : \" + result);
}
}
Solution
Output is:
a: 10
a and b: 15 , 25
40
double a: 7.7
Result: 15.4
first of all add method will be called with one argument int and value of a will be printed.
then add method with 2 arguments will be called and value of a and b will be printed an then value of sum.
after that add method with will called with double argument and value of double a will be printed and then Result.

