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?
- interface Drinkable {
- void drink();
- }
- class Tea implements Drinkable {
- public void drink() { System.out.println("Drinking tea..."); }
- }
- class Coffee implements Drinkable {
- public void drink() { System.out.println("Drinking coffee..."); }
- }
- public class TestDrink {
- static boolean teaflag;
- public static Drinkable getDrinkable() {
- if (teaflag) {
- return new Tea();
- } else {
- return new Coffee();
- }
- }
- // more code that sets the value of the teaflag boolean
- }
- public class DrinkableFactory {
- static boolean teaflag;
- public static Drinkable getDrinkable() {
- if (teaflag) {
- return new Tea();
- } else {
- return new Coffee();
- }
- }
- // more code that sets the value of the teaflag boolean
- }
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.