Which code, inserted at line 16, will cause a java.lang.ClassCastException?

Given:
10. interface Foo {}
11. class Alpha implements Foo {}
12. class Beta extends Alpha {}
13. class Delta extends Beta {
14. public static void main( String[] args ) {
15. Beta x = new Beta();
16. // insert code here
17. }
18. }
Which code, inserted at line 16, will cause a java.lang.ClassCastException?

Given:
10. interface Foo {}
11. class Alpha implements Foo {}
12. class Beta extends Alpha {}
13. class Delta extends Beta {
14. public static void main( String[] args ) {
15. Beta x = new Beta();
16. // insert code here
17. }
18. }
Which code, inserted at line 16, will cause a java.lang.ClassCastException?

A.
Alpha a = x;

B.
Foo f = (Alpha)x;

C.
Beta b = (Beta)(Alpha)x;

D.
Foo f = (Delta)x;



Leave a Reply 0

Your email address will not be published. Required fields are marked *