ProgrammingModerateTCS NQT · Infosys interview pattern

Which line fails to compile?

Which line fails to compile?
abstract class Shape { abstract double area(); }
class Sq extends Shape { double area() { return 4; } }
// in main:
Shape s1 = new Sq();          // L1
Shape s2 = new Shape();       // L2
Shape[] arr = new Shape[3];   // L3
  1. A. L1
  2. B. L3
  3. C. L2
  4. D. None of them

Correct answer: C. L2

An abstract class cannot be instantiated, so new Shape() fails. L1 uses a concrete subclass through a base reference, and L3 only creates an array of null references of type Shape, which is allowed.

More OOP with C++ and Java questions

Exams that ask this topic