nerdexam
Oracle

1Z0-851 · Question #77

Given: class Pizza { java.util.ArrayList toppings; public final void addTopping(String topping) { toppings.add(topping); } } public class PepperoniPizza extends Pizza { public void addTopping(String…

The correct answer is A. Compilation fails. See the full explanation below for the reasoning.

Question

Given: class Pizza { java.util.ArrayList toppings; public final void addTopping(String topping) { toppings.add(topping); } } public class PepperoniPizza extends Pizza { public void addTopping(String topping) { System.out.println("Cannot add Toppings"); } public static void main(String[] args) { Pizza pizza = new PepperoniPizza(); pizza.addTopping("Mushrooms"); } } What is the result?

Exhibit

1Z0-851 question #77 exhibit

Options

  • ACompilation fails.
  • BCannot add Toppings
  • CThe code runs with no output.
  • DA NullPointerException is thrown in Line 4.

How the community answered

(17 responses)
  • A
    82% (14)
  • B
    6% (1)
  • C
    12% (2)

Community Discussion

No community discussion yet for this question.

Full 1Z0-851 Practice