Java SE 8 Programmer II — Question 29
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?
Answer options
- A. Car auto = Car ("MyCar"): : new;
- B. Car auto = Car : : new; Car vehicle = auto : : getCar("MyCar");
- C. Rideable rider = Car : : new; Car vehicle = rider.getCar("MyCar");
- D. Car vehicle = Rideable : : new : : getCar("MyCar");
Correct answer: C
Explanation
Option C is correct because it creates an instance of Car through the Rideable interface and successfully calls the getCar method. Option A has incorrect syntax for instantiating a Car object. Option B tries to use a Car instance to invoke getCar, which is not valid since getCar is defined in the Rideable interface. Option D incorrectly attempts to call getCar directly on the Rideable interface, which is not possible.