nerdexam
Oracle

1Z0-850 · Question #7

1Z0-850 Question #7: Real Exam Question with Answer & Explanation

Sign in or unlock 1Z0-850 to reveal the answer and full explanation for question #7. The question stem and answer options stay visible for context.

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();

Unlock 1Z0-850 to see the answer

You've previewed enough free 1Z0-850 questions. Unlock 1Z0-850 for full answers, explanations, the timed quiz mode, progress tracking, and the master PDF. Question stem and options stay visible so you can still see what's on the exam.

Full 1Z0-850 Practice