ProgrammingHardTCS NQT · Infosys pattern

What happens?

What happens?
struct Shape {
    virtual double area() const = 0;
};
int main() {
    Shape s;
    cout << s.area();
}
  1. A. It compiles and prints 0
  2. B. Compilation error: cannot declare an object of abstract class type Shape
  3. C. It throws a run-time exception
  4. D. It compiles and prints a garbage value

Correct answer: B. Compilation error: cannot declare an object of abstract class type Shape

A class with any pure virtual function is abstract and can never be instantiated directly, even as a local stack object; the compiler rejects `Shape s;` outright as a compile-time error, long before execution could reach s.area().

More OOP with C++ and Java questions

Exams that ask this topic