Question 1
What does this print? ```java public class Main { record Pair(int a, int b) {} public static void main(String[] args) { Object o = new Pair(2, 3); if (o instanceof Pair(int a, int b)) { System.out.println(a * b); } } } ```
A. 6Correct answer
The value is a Pair, so o instanceof Pair(int a, int b) matches and binds a=2, b=3; the body prints a * b = 6 (JEP 440).
B. Compilation fails: record pattern not allowed in instanceof
Assumes record patterns are switch-only, but JEP 440 explicitly extends them to instanceof, so the code compiles.
C. 23
23 would be string concatenation of the two components, but a * b is integer multiplication.
D. 5
5 is a + b, the sum, not the product that the code computes.
Explanation
Record deconstruction is not limited to switch; a record pattern can also appear in an instanceof test, where a matching value is deconstructed and its component bindings are flow-scoped into the guarded block. Here the value is that record type, so the pattern matches and both components are bound. The body multiplies them, so the output is their product.