Java instanceof Pattern Matching and Method Invocation
Given: What is the result? - 
Community Votes
100% of anonymous learners picked answer D. Votes are pick records left by other test-takers — they are not the verified answer.
Community Insight
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.
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.
Community Discussion (5 comments)
Comments & Corrections
No comments yet — spotted an error or have a note? Share it below.
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 calledcobj.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 thatcobj 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?
Because C does not override mB(), the inherited implementation from B is used. Polymorphism only applies to overridden methods.
Can I call v.mC() directly after binding v to B in the first if?
No. The variable v is statically typed as B. To call mC(), you must re-bind v to a variable of type C using another instanceof check, as shown with v1.