ProgrammingHardProduct-company placement · Java

Which line fails to compile?

Which line fails to compile?
abstract class Shape { abstract double area(); }
// in main:
Shape a = new Shape();                                    // L1
Shape b = new Shape() { double area() { return 1; } };    // L2
Shape[] c = new Shape[3];                                 // L3
  1. A. L2 only
  2. B. L1 and L3
  3. C. L1 and L2
  4. D. L1 only

Correct answer: D. L1 only

An abstract class cannot be instantiated directly, but an anonymous subclass that implements area() can, and an array of an abstract type only creates references (all null).

More OOP with C++ and Java questions

Exams that ask this topic