Oracle
1Z0-851 · Question #147
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()…
The correct answer is B. Foo f = (Delta)x. See the full explanation below for the reasoning.
Question
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?
Options
- AAlpha a = x;
- BFoo f = (Delta)x;
- CFoo f = (Alpha)x;
- DBeta b = (Beta)(Alpha)x;
How the community answered
(29 responses)- A10% (3)
- B79% (23)
- C7% (2)
- D3% (1)
Community Discussion
No community discussion yet for this question.