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());- A. 1.0
- B. Compilation error: static interface methods cannot return a lambda
- C. 0.0
- 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
- What is printed? java class Shape { protected String name = "Shape"; Shape() { System.out.print(name + " "); } } class Circle extends…
- What is printed? cpp class Account { double balance; public: explicit Account(double b) : balance(b) {} double get() const { return…
- What is printed? java class Base { protected Object clone2() { return new Base(); } } class Sub extends Base { @Override public Object…
- What is printed? java class Shape { double area() { return 0.0; } } class Rectangle extends Shape { double w, h; Rectangle(double w…
- What is printed? java class Vehicle implements Comparable<Vehicle> { int speed; Vehicle(int s) { speed = s; } public int compareTo(Vehicle…
- What is printed? cpp struct Base { virtual Base& self() { cout << "Base::self "; return this; } }; struct Derived : Base { Derived& self()…