Exam 1z0-809 All QuestionsBrowse all questions from this exam
Question 7

Given:

class Bird {

public void fly () { System.out.print("Can fly"); }

}

class Penguin extends Bird {

public void fly () { System.out.print("Cannot fly"); }

}

and the code fragment:

class Birdie {

public static void main (String [ ] args) {

fly( ( ) -> new Bird ( ));

fly (Penguin : : new);

}

/* line n1 */

}

Which code fragment, when inserted at line n1, enables the Birdie class to compile?

    Correct Answer: C

    The correct answer involves defining the fly method to accept a Supplier of Bird objects. A Supplier is a functional interface that supplies objects of a given type. In this case, it seamlessly handles both Bird and Penguin instances. The correct fly method syntax is static void fly(Supplier<Bird> bird) { bird.get().fly(); }, ensuring that the get method retrieves a Bird object, on which the fly method can then be invoked.

Discussion
mevltOption: C

The answer is C but it must have a period(.) after bird.get(). Eventually bird.get().fly()

pul26Option: C

package birdie; import java.util.function.Supplier; class Bird { public void fly() { System.out.print("Can fly"); } } class Penguin extends Bird { public void fly() { System.out.print("Cannot fly"); } } public class Birdie { public static void main(String[] args) { fly(() -> new Bird()); fly(Penguin::new); } static void fly(Supplier<Bird> bird) { bird.get().fly(); } }

YasinGaberOption: C

Compiling the code with answer C (it is the right answer) results in following output: Can flyCannot fly

asdfjhfgjuaDCVOption: C

C is the answer

steefaandOption: C

Answer is C

r1muka5Option: C

Answer is C.

dexdinh91Option: C

lack a dot, shoule be compile error

WilsonKKerllOption: C

public class ch7 { public static void main(String[] args) { fly(() -> new Bird()); fly(Penguin::new); } static void fly(Supplier<Bird> bird) { bird.get().fly(); } } class Bird { public void fly() { System.out.println("Can fly"); } } class Penguin extends Bird { public void fly() { System.out.println("Cannot fly"); } }

AVB22Option: C

C, tested