1Z0-829 · Question #14
Given: 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");}}…
The correct answer is E. mA. Option E (mA) is listed as correct, but this answer key contains an error - the program actually prints mB. Since C extends B, the runtime type of cObj (which is C) satisfies instanceof B, making the condition true; the if branch executes and v.mB() prints "mB", never reaching…
Question
Options
- AMb
- BMC
- CMb
- DMA
- EmA
How the community answered
(49 responses)- A2% (1)
- B2% (1)
- C6% (3)
- D12% (6)
- E78% (38)
Explanation
Option E (mA) is listed as correct, but this answer key contains an error - the program actually prints mB. Since C extends B, the runtime type of cObj (which is C) satisfies instanceof B, making the condition true; the if branch executes and v.mB() prints "mB", never reaching the else branch that would call cObj.mA().
Why each distractor fails:
- A/C (Mb): These appear to be the same as the real answer ("mB"), suggesting a case-sensitivity formatting error in the choices; the actual output
"mB"comes fromSystem.out.println("mB"). - B (MC/mC):
mC()is never called; nothing in the code invokes it. - D (MA/mA): This would only print if the
instanceof Bcheck were false - but since C is a subtype of B, it is always true, so the else branch is unreachable.
Memory tip: For instanceof with pattern matching, remember the chain: if C extends B, then every C is also a B (and also an A). Always trace the full inheritance chain before evaluating the condition - if the runtime type is a descendant of the tested type, the check passes.
Note to exam takers: Verify the answer key with your instructor - based on Java semantics, the correct output should be
"mB", not"mA".
Topics
Community Discussion
No community discussion yet for this question.