Oracle
1Z0-851 · Question #127
Given: 05. class Building { } 06. public class Barn extends Building { 07. public static void main(String[] args) { 08. Building build1 = new Building(); 09. Barn barn1 = new Barn(); 10. Barn barn2…
The correct answer is C. If line 12 is removed, the compilation succeeds. Cannot cast from Building to String
Class Design
Question
Given: 05. class Building { } 06. public class Barn extends Building { 07. public static void main(String[] args) { 08. Building build1 = new Building(); 09. Barn barn1 = new Barn(); 10. Barn barn2 = (Barn) build1; 11. Object obj1 = (Object) build1; 12. String str1 = (String) build1; 13. Building build2 = (Building) barn1; 14. } 15. } Which is true?
Options
- AIf line 10 is removed, the compilation succeeds.
- BIf line 11 is removed, the compilation succeeds.
- CIf line 12 is removed, the compilation succeeds.
- DIf line 13 is removed, the compilation succeeds.
- EMore than one line must be removed for compilation to succeed.
How the community answered
(52 responses)- A2% (1)
- B15% (8)
- C73% (38)
- D6% (3)
- E4% (2)
Explanation
Cannot cast from Building to String
Topics
#type casting#downcasting#compile-time error#class hierarchy
Community Discussion
No community discussion yet for this question.