Exam 1z0-829 All QuestionsBrowse all questions from this exam
Question 12

Given:

What is the result?

    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
james2033Option: 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

supersquaxOption: D

D is correct, verified in online java 17 compiler.

UtemanOption: D

D is correct

xplorerpjOption: D

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)

minhdevOption: D

D is correct answer.