ProgrammingHardWipro · Cognizant pattern

What is printed?

What is printed?
interface Shape {
    double area();
    static Shape unitSquare() { return () -> 1.0; }
}
// in main:
Shape s = Shape.unitSquare();
System.out.println(s.area());
  1. A. 1.0
  2. B. Compilation error: static interface methods cannot return a lambda
  3. C. 0.0
  4. D. Compilation error: interfaces cannot declare static methods

Correct answer: A. 1.0

Interfaces may declare static methods (since Java 8), and since Shape has exactly one abstract method (area()), it is a valid functional interface, so `() -> 1.0` legally implements it; unitSquare() returns that lambda-backed Shape, and s.area() correctly prints 1.0.

More OOP with C++ and Java questions

Exams that ask this topic