What will be the output of the code given below?

class Calculator {
	public int num1 = 10;
	public float num2 = 20;
	public double sum;
	public double product;

	public double add(int num1, int num2) {
		double sum = this.num1 + this.num2;
		return sum;
	}

	public double multiply(int num1, int num2) {
		double sum = this.add(num1, num2);
		double product = this.sum * this.num1;
		return (int) product;
	}
}

public class Tester {
	public static void main(String[] args) {
		Calculator c = new Calculator();
		System.out.println(c.multiply(10, 5));
	}

}

150.0

300.0

0

0.0

Verified Answer
Correct Option - d
Anush said

The output of the code will be 0.0. Here's why:
1. In the add method, the sum is calculated using the instance variables this.num1 (which is 10) and this.num2 (which is 20.0, as num2 is a float). Therefore, sum = 10 + 20 = 30.0, and this value is returned by the add method.
2. In the multiply method, the sum is again calculated by calling this.add(num1, num2), which returns 30.0.
3. The product is then calculated as this.sum * this.num1, but this.sum has never been assigned a value and defaults to 0.0 (since sum is declared as a double). So, product = 0.0 * 10 = 0.0.
4. Finally, (int) product is returned, which results in 0 being printed as product is 0.0.

To get all Self Assessment - Programming using Java Exam questions Join Telegram Group https://rebrand.ly/lex-telegram-236dee

Telegram

We're passionate about offering best placement materials and courses!! A one stop place for Placement Materials. We daily post Offcampus updates and Placement Materials.

Qtr No. 213, New Town Yehlanka Indore 454775

admin@prepflix.in

Updated on Mon, 8 Dec 2025