1z0-809 Exam QuestionsBrowse all questions from this exam

1z0-809 Exam - Question 43


Given:

interface Rideable {Car getCar (String name); }

class Car {

private String name;

public Car (String name) {

this.name = name;

}

}

Which code fragment creates an instance of Car?

Show Answer
Correct Answer: C

The correct answer is the option where Rideable is assigned to Car::new, and then the getCar method is called with the argument 'MyCar'. This works because Rideable is a functional interface with a single abstract method, getCar. By using Car::new, we are effectively implementing the getCar method to return a new Car instance. Therefore, when rider.getCar('MyCar') is called, it creates a new Car object with the name 'MyCar'.

Discussion

6 comments
Sign in to comment
M_JawadOption: C
Jan 14, 2020

C is the Correct Answer

adnano1234
Jan 14, 2020

True...The interface must be functional

WilsonKKerllOption: C
Mar 12, 2022

Answer is C.

r1muka5Option: C
Mar 14, 2023

Answer is C

asdfjhfgjuaDCVOption: C
Mar 6, 2024

C is the correct answer

DarGrinOption: C
May 25, 2024

Why does it work? Rideable rider = Car::new; Class Car doesn't implement Interface Rideable! Can somebody explain it please

Syyyyyyyy
May 30, 2024

Rideable is a FunctionalInterface. Rideable rider = Car::new, assigns Car::new as rider's getCar() method. So when you call rider.getCar() you are calling Car::new.