nerdexam
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)
  • A
    2% (1)
  • B
    15% (8)
  • C
    73% (38)
  • D
    6% (3)
  • E
    4% (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.

Full 1Z0-851 Practice