LearnFrenzy
Sign in Take a free test

Classes and Objects · Predict the Output · Question 11

An anonymous class implementing an interface Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 11 Back to Exercise

What is the output of the following Java program?

Main.java
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links