Exam 1z0-808 All QuestionsBrowse all questions from this exam
Question 176

Given:

What is the result?

    Correct Answer: D

    In the given code, we are dealing with method overriding and polymorphism. The variable types may be different but the actual objects are of specific subclasses. Firstly, b1 is assigned to a DerivedB object, but later it is cast to refer to b2, which is a DerivedA object. When b1.test() is called, it invokes the test method of DerivedA. Secondly, b4 references b3, which is a DerivedB object. Thus, calling b4.test() invokes the test method of DerivedB. Therefore, the output will be: DerivedA DerivedB.

Discussion
amit_lad88

The options are incorrect. The correct answer is DerivedA DerivedB. tested in a local machine.

pbbvrOption: D

DerivedA DerivedB should be the correct answer. Tested!

Ericausdresden

I dont understand why the first output isnt Base... why does the cast to Base not work...

iSnoverOption: B

This question has no correct alternative as it prints "DerivedA DerivedB". What you have to pay in these questions is to which object the variable is pointing and we see this in the "new DerivedB", so we know that our B1 and B3 even being of the "Base" type they are "DerivedB" objects. the variable B4 is a reference of B3 which is a "DerivedB" object so we know that in the second position of the print it is a "DerivedB", even doing a cast of B2 -> Base the overload always takes the child's method then prints "DerivedA " getting "DerivedA DerivedB". To be honest, I don't know what to mark if this happens in the test, but I would mark the alternative that came closest to the truth, which is alternative B. The only one that came closest to the "b1.test()" method and hit the " b4.test()". If in doubt, follow the code:

iSnover

class Base { public void test() { System.out.println("Base "); } } class DerivedA extends Base { public void test() { System.out.println("DerivedA "); } } public class DerivedB extends DerivedA { public void test() { System.out.println("DerivedB "); } public static void main(String[] args) { Base b1 = new DerivedB(); Base b2 = new DerivedA(); Base b3 = new DerivedB(); Base b4 = b3; b1 = (Base) b2; b1.test(); b4.test(); } }

BelloMioOption: D

I tested as well after I thought I was going crazy. Correct answer is DerivedA DerivedB class DerivedB extends DerivedA { public void test() { System.out.println("DerivedB "); } public static void main(String[] args) { Base b1 = new DerivedB(); Base b2 = new DerivedA(); Base b3 = new DerivedB(); Base b4 = b3; b1 = (Base) b2; b1.test(); b4.test(); } }

7df49fbOption: D

DerivedA DerivedB

carloswork

Answer is "DerivedA DerivedB". -------------------------------------------------- // Base.java public class Base { public void test() { System.out.println("Base "); } } ------------------------- // DerivedA.java class DerivedA extends Base { public void test() { System.out.println("DerivedA "); } } ------------------------- // DerivedB.java class DerivedB extends DerivedA { public void test() { System.out.println("DerivedB "); } public static void main(String[] args) { Base b1 = new DerivedB(); Base b2 = new DerivedA(); Base b3 = new DerivedB(); Base b4 = b3; b1 = (Base) b2; b1.test(); b4.test(); } } --------------------------------------------------