ProgrammingHardProduct-company MCQ pattern

Which line fails to compile?

Which line fails to compile?
struct Shape  { virtual double area() = 0; };
struct Circle : Shape { };
struct Square : Shape { double area() override { return 1; } };
  1. A. Shape *p = new Square;
  2. B. Shape *q = nullptr;
  3. C. Square s;
  4. D. Circle c;

Correct answer: D. Circle c;

A class with a pure virtual function is abstract and cannot be instantiated. Circle does not override area, so it inherits the pure virtual function and stays abstract. Pointers to an abstract type are fine.

More OOP with C++ and Java questions

Exams that ask this topic