1z0-808 Exam QuestionsBrowse all questions from this exam

1z0-808 Exam - Question 176


Given:

What is the result?

Show Answer
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

7 comments
Sign in to comment
iSnoverOption: B
Oct 9, 2022

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
Oct 9, 2022

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(); } }

Ericausdresden
Jul 29, 2023

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

pbbvrOption: D
Aug 9, 2023

DerivedA DerivedB should be the correct answer. Tested!

amit_lad88
Dec 19, 2023

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

carloswork
Nov 18, 2022

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(); } } --------------------------------------------------

7df49fbOption: D
Mar 21, 2024

DerivedA DerivedB

BelloMioOption: D
Jun 5, 2024

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(); } }