Question discussion

Consider the below Code snippet:What will be the output?

359036Medium DSSSB TGT Computer Science 0 replies 0 views
0
Consider the below Code snippet:
What will be the output?
class Calculator {
    void calculate(int a, int b) {
        class Multiply {
            int product() {
                return a * b;
            }
        }
        Multiply multiply = new Multiply();
        System.out.println("Product: " + multiply.product());
    }
}
public class Main {
    public static void main(String[] args) {
        Calculator calculator = new Calculator();
        calculator.calculate(5, 3);
    }
}
  • AProduct: 15
  • BProduct: 0
  • CRuntime Error.
  • DCompilation Error: Multiply cannot be defined inside calculate.
Correct answer: A

Explanation

A local inner class can use the method's parameters if they are effectively final. So product() returns 5 × 3, and the output is "Product: 15".

Discussion 0

Nobody has replied yet. Start the discussion!