What will be the output of the code given below?

public class Calculator{
    private int add(int num1, int num2){
        return num1+num2;
    }   
}

public class Tester{
    public static void main(String args[]) {
		Calculator calculator = new Calculator();
		System.out.println(calculator.add(1,2));
	}
}

3

The code will result in a compilation error as the method add cannot be accessed outside the class.

The code will not result in a compilation error but will not display any output.

The code will result in a compilation error as a non-static method cannot be invoked from a static method.

Verified Answer
Correct Option - b
Anush said

The correct answer is: The code will result in a compilation error as the method add cannot be accessed outside the class.
Here's why:
1. The add method in the Calculator class is declared as private, meaning it can only be accessed within the Calculator class.
2. In the Tester class, the code tries to access the add method from outside the Calculator class, which results in a compilation error due to the access level restriction.
3. The error is not related to static methods or output, but simply due to the private access modifier on the add method.

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