Question 11
Back to Exercise
What is the output of the following Java program?
interface Discount { double apply(double price); }
public class Main {
public static void main(String[] args) {
Discount festival = new Discount() {
@Override
public double apply(double price) { return price * 0.8; }
};
Discount flat = price -> price - 50; // the same idea as a lambda
double p = 500;
System.out.println(festival.apply(p) + " " + flat.apply(p) + " " + flat.apply(festival.apply(p)));
}
}
- A 400.0 450.0 350.0 Correct
- B 400.0 450.0 360.0
- C 100.0 50.0 50.0
- D 400 450 350
Correct Answer: Option A
The anonymous class and the lambda are two ways of creating an object that implements Discount.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!