1z0-829 Exam QuestionsBrowse all questions from this exam

1z0-829 Exam - Question 12


Given:

What is the result?

Show Answer
Correct Answer: D

Given the classes A, B, and C, where B extends A and C extends B, and examining the method calls made in the main method, we can deduce the output. The variable 'cobj' is declared as type A and instantiated as an object of class C. The 'if' condition checks whether 'cobj' is an instance of B, which is true because C is a subclass of B. Therefore, 'v.mB()' is called, which prints 'mB'. Next, it checks if 'v' is an instance of C, which is also true, so 'v1.mC()' is called, printing 'mC'. Thus, the final output is 'mB' followed by 'mC'.

Discussion

5 comments
Sign in to comment
supersquaxOption: D
Jan 25, 2024

D is correct, verified in online java 17 compiler.

james2033Option: D
Feb 15, 2024

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

minhdevOption: D
May 5, 2024

D is correct answer.

xplorerpjOption: D
Jun 16, 2024

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)

UtemanOption: D
Jul 2, 2024

D is correct