nerdexam
Oracle

1Z0-851 · Question #36

Given: class Animal { public String noise() { return "peep"; } } class Dog extends Animal { public String noise() { return "bark"; } } class Cat extends Animal { public String noise() { return…

The correct answer is E. An exception is thrown at runtime. Exception in thread "main" java.lang.ClassCastException: Dog cannot be cast to Cat at Client.main(Client.java:12)

Analyzing and Troubleshooting System Problems

Question

Given: class Animal { public String noise() { return "peep"; } } class Dog extends Animal { public String noise() { return "bark"; } } class Cat extends Animal { public String noise() { return "meow"; } } 30. Animal animal = new Dog(); 31. Cat cat = (Cat)animal; 32. System.out.println(cat.noise()); What is the result?

Options

  • Apeep
  • Bbark
  • Cmeow
  • DCompilation fails.
  • EAn exception is thrown at runtime.

How the community answered

(27 responses)
  • A
    11% (3)
  • B
    4% (1)
  • C
    4% (1)
  • D
    4% (1)
  • E
    78% (21)

Explanation

Exception in thread "main" java.lang.ClassCastException: Dog cannot be cast to Cat at Client.main(Client.java:12)

Topics

#Java casting#class hierarchy#ClassCastException#polymorphism

Community Discussion

No community discussion yet for this question.

Full 1Z0-851 Practice