Java instanceof Pattern Matching and Method Invocation
Short answer
D The program prints "mB" followed by "mC" due to successful instanceof matches and non-overridden method calls.
Question

Key Takeaways
The question tests whether candidates understand that a pattern-matched variable (v) can be used to invoke methods defined in its matched type (B), even if the runtime object is a subclass (C). The common trap is assuming the variable must be downcast explicitly or that only methods of the most specific type are accessible.
This Java 17 exam question tests the interaction between instanceof pattern matching, variable binding, and polymorphic method calls. It establishes that a bound variable retains its static type for compilation checks while allowing dynamic dispatch for instance-specific methods.
Common mistake: Candidates often select A (mA) because they believe the reference 'v' should strictly adhere to the declared type 'A' or because they misunderstand how instanceof binding affects method resolution. Others might choose C (mC) by assuming the inner check triggers the outer block incorrectly or ignoring the output order.
Expert Analysis
Why the Answer Is Correct
The code uses Java 17's instanceof pattern matching: if (cobj instanceof B v). Here, cobj is of static type A but runtime type C. Since C extends B, the condition is true, and v is bound as a variable of type B. Inside this block, v.mB is called. Although v is statically typed as B, the actual object is an instance of C. However, B defines mB, and C does not override it (it only adds mC). Therefore, B.mB is executed, printing "mB". Next, if (v instanceof C v1) checks if v (the C instance) is a C. This is true, so v1 is bound as type C, and v1.mC is called, printing "mC". The else block is skipped. The output is "mB mC".
Why the Other Options Are Wrong
Option A (mA) would only occur if the first instanceof failed or if we called cobj.mA directly without overriding issues, but here the flow enters the if blocks. Option B (just mB) ignores the second successful instanceof check for C. Option C (just mC) implies mB wasn't printed, which contradicts the explicit call v.mB before the nested check.
Community Comment Notes
Community consensus strongly supports D. As user james2033 noted, the code prints "mB" then "mC". User supersquax verified the result using an online Java 17 compiler. User xplorerpj explained that cobj instanceof B is true and v instanceof C is also true, leading to both outputs. User Uteman and minhdev simply confirmed D is correct based on execution.
Exam Strategy
When analyzing instanceof pattern matching, always identify the static type of the bound variable. Remember that you can invoke methods available in that static type, even if the runtime object is more specific. Be careful with method overriding; if the subclass overrides the method, the subclass version runs; if not, the superclass version runs.
Frequently Asked Questions
Why does v.mB() print from class B if v is actually a C instance?
Can I call v.mC() directly after binding v to B in the first if?
Community Votes
100% of anonymous learners picked answer D. Votes are pick records left by other test-takers — they are not the verified answer.
Discussion 5 contributions
Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.
From public community threads 5
-
james2033 👍 2 Answered Dpackage q12; class A { public void mA() { System.out.println("mA"); } } class B extends A { public void mB() { System.out.println("mB"); } } class C extends B { public void mC() { System.out.println("mC"); } } public class App { public static void main(String[] args) { A bobj = new B(); A cobj = new C(); if (cobj instanceof B v) { v.mB(); if (v instanceof C v1) { v1.mC(); } else { cobj.mA(); } } } } // Result: // mB // mC
-
supersquax 👍 2D is correct, verified in online java 17 compiler.
-
xplorerpj 👍 1D is correct answer cobj instanceof B == true , because class C extends B v instanceof C == true, because here we compare class C instance to Class C instance OutPut: mB mC (Also tested the code by running)
-
minhdev 👍 1D is correct answer.
-
Uteman 👍 1D is correct
Reader comments & corrections
No reader corrections yet — spotted an error or have a note? Share it below.