Suppose you have two classes defined as follows. (They are not complete to save space): class ApBase extends Object implements Runnable { } class ApDerived extends ApBase implements Observer { } Also suppose you have two variables created as follows: ApBase aBase = new ApBase() ; ApDerived aDer = new ApDerived() ; Which of the following Java statements will compile and execute without error? [Check all correct answers]

Back to Questions

Runnable rn = aDer ;


Runnable rn2 = (Runnable) aBase ;


Observer ob = aBase ;


Observer ob2 = (Observer) aBase ;


Author Explanation :

Answers A and B are correct. Answer A is correct because the ApDerived class inherits from ApBase, which implements Runnable. Answer B is correct because the inserted cast (Runnable) is not needed but it does not cause a problem. Answer C fails to compile because the compiler can tell that the ApBase class does not implement Observer. Therefore, answer C is incorrect. Answer D is incorrect because it compiles but fails to execute. Because of the specific cast, the compiler thinks you know what you are doing, but the type of the aBase reference is checked when the statement executes and a ClassCastException is thrown.

Answers Posted

PostedBy By: subbu. kurapati, at 10:05 AM on 28-March-2019

Answer Posted: A

Explanation: Parent reference can hold child. But child can't hold parent reference

Result: Wrong Answer

Back to Questions

© Copyright 1997 - 2019 | | | All Rights Reserved.
Bestdotnet google plus
Enquiry Now