nerdexam
Oracle

1Z0-850 · Question #7

Click the Exhibit button. Which, inserted at line 3 of the TestDrink class, demonstrates the "program to an interface" principle? 1. interface Drinkable { 2. void drink(); 3. } 1. class Tea…

The correct answer is A. Drinkable aDrink = DrinkableFactory.getDrinkable(). See the full explanation below for the reasoning.

Question

Click the Exhibit button. Which, inserted at line 3 of the TestDrink class, demonstrates the "program to an interface" principle?
  1. interface Drinkable {
  2. void drink();
  3. }
  4. class Tea implements Drinkable {
  5. public void drink() { System.out.println("Drinking tea..."); }
  6. }
  7. class Coffee implements Drinkable {
  8. public void drink() { System.out.println("Drinking coffee..."); }
  9. }
  10. public class TestDrink {
  11. static boolean teaflag;
  12. public static Drinkable getDrinkable() {
  13. if (teaflag) {
  14. return new Tea();
  15. } else {
  16. return new Coffee();
  17. }
  18. }
  19. // more code that sets the value of the teaflag boolean
  20. }
  21. public class DrinkableFactory {
  22. static boolean teaflag;
  23. public static Drinkable getDrinkable() {
  24. if (teaflag) {
  25. return new Tea();
  26. } else {
  27. return new Coffee();
  28. }
  29. }
  30. // more code that sets the value of the teaflag boolean
  31. }

Options

  • ADrinkable aDrink = DrinkableFactory.getDrinkable();
  • BDrinkable aDrink = new Drinkable();
  • CTea aDrink = new Drinkable();
  • DTea aDrink = new Tea();

How the community answered

(56 responses)
  • A
    80% (45)
  • B
    5% (3)
  • C
    11% (6)
  • D
    4% (2)

Community Discussion

No community discussion yet for this question.

Full 1Z0-850 Practice