nerdexam
Oracle

1Z0-851 · Question #12

Given: class Alpha { public void foo() { System.out.print("Afoo "); } } public class Beta extends Alpha { public void foo() { System.out.print("Bfoo "); } public static void main(String[] args) {…

The correct answer is D. Bfoo Bfoo. See the full explanation below for the reasoning.

Question

Given: class Alpha { public void foo() { System.out.print("Afoo "); } } public class Beta extends Alpha { public void foo() { System.out.print("Bfoo "); } public static void main(String[] args) { Alpha a = new Beta(); Beta b = (Beta)a; a.foo(); b.foo(); } } What is the result?

Options

  • AAfoo Afoo
  • BAfoo Bfoo
  • CBfoo Afoo
  • DBfoo Bfoo
  • ECompilation fails.
  • HAn exception is thrown at runtime.

How the community answered

(21 responses)
  • A
    5% (1)
  • C
    10% (2)
  • D
    81% (17)
  • H
    5% (1)

Community Discussion

No community discussion yet for this question.

Full 1Z0-851 Practice