Question 32Asked in DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 3 (5:00 PM)

Consider the below Code snippet: class Calculator {

Consider the below Code snippet: 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); } } What will be the output?
  1. AProduct: 15
  2. BProduct: 0
  3. CRuntime Error.
  4. DCompilation Error: Multiply cannot be defined inside calculate.
Show answerHide answer

Correct answer: (A) Product: 15

Explanation

Java allows a local class inside a method, and it can use the method's effectively final parameters a and b. product() returns 5 × 3 = 15, so "Product: 15" is printed.

From DSSSB TGT Computer Science PYQ (Technical), 12 Sep 2025, Shift 3 (5:00 PM)

This is question 32 of DSSSB TGT Computer Science PYQ (Technical), 12 Sep 2025, Shift 3 (5:00 PM). See all questions of this paper with answers, or attempt the paper as a timed online test to get your score and all-India rank.