ProgrammingHardTCS NQT · Infosys pattern
What happens?
What happens?
struct Shape {
virtual double area() const = 0;
};
int main() {
Shape s;
cout << s.area();
}- A. It compiles and prints 0
- B. Compilation error: cannot declare an object of abstract class type Shape
- C. It throws a run-time exception
- 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
- What is printed? java abstract class Vehicle { abstract void move(); void start() { System.out.print("starting "); move(); } } // in main…
- What is printed? cpp class Base { public: Base(int x) { cout << "Base(" << x << ") "; } }; class Derived : public Base { public: Derived()…
- What is printed? java class Outer { static class Nested { void show() { System.out.print("Nested.show"); } } } // in main: Outer.Nested n…
- What is printed? java interface Shape { double area(); static Shape unitSquare() { return () -> 1.0; } } // in main: Shape s =…
- 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…