Java instanceof Pattern Matching and Method Invocation

Utilizing Java Object-Oriented Approach
Answer Correct answer: D — The program prints "mB" followed by "mC" due to successful instanceof matches and non-overridden method calls.

Given: What is the result? - image

  1. mA
  2. mB
  3. mB
  4. mB Correct Answer

Community Votes

D
100%

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)

Uteman 👍 1
D is correct
xplorerpj 👍 1
D 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 👍 1
D is correct answer.
james2033 👍 2 Selected: D
package 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 👍 2
D is correct, verified in online java 17 compiler.

Comments & Corrections

No comments yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Submitted for moderation before publishing. Keep it helpful and respectful.

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?

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.

More 1Z0-829 FAQ →

Related Analysis

← Back to 1Z0-829 Study Guide