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

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 */

}

line n1, enables the Birdie class to compile?

Which code fragment, when inserted at

    Correct Answer: C

    The method 'fly' should take a functional interface that returns a Bird object, and a Supplier<Bird> fits this requirement because it supplies (or returns) a Bird when invoked via its get() method. The syntax 'bird.get().fly();' accesses the fly method of the Bird object produced by the Supplier. This way, when Supplier<Bird> bird = () -> new Bird() is passed, bird.get() produces a Bird instance which can then call the fly method. Similarly, passing Penguin::new as an argument would produce a Penguin instance, which also has a fly method. Option C is correct as it correctly incorporates a Supplier<Bird> and uses the correct method call to enable the Birdie class to compile and run, following the expected behavior of method references and lambda expressions in this context.

Discussion
Ritesh_Option: C

Correct.Answer is C.

Abdullah_Rahahleah

C and D are correct

AVB22

I confirm, it compiles with both c and d but we cannot see the body of d

steefaandOption: C

C and D both compile.