C++ OOP: Polymorphism Quiz
40 in-depth questions covering C++ polymorphism fundamentals including virtual functions, pure virtual functions, abstract classes, virtual destructors, and dynamic dispatch — with 16 code examples to master runtime polymorphism patterns.
Question 1
What is polymorphism in C++?
Question 2
What is a virtual function in C++?
class Base {
public:
virtual void method() { // Virtual function
std::cout << "Base method" << std::endl;
}
};
class Derived : public Base {
public:
void method() override { // Override
std::cout << "Derived method" << std::endl;
}
};
int main() {
Base* b = new Derived();
b->method(); // Calls Derived::method() at runtime
delete b;
return 0;
}Question 3
What is the vtable (virtual table) in C++?
Question 4
What is a pure virtual function in C++?
class AbstractShape {
public:
virtual double area() const = 0; // Pure virtual
virtual ~AbstractShape() = default;
void draw() const { // Non-pure virtual with implementation
std::cout << "Drawing shape" << std::endl;
}
};
class Circle : public AbstractShape {
public:
Circle(double r) : radius(r) {}
double area() const override {
return 3.14159 * radius * radius;
}
private:
double radius;
};
int main() {
// AbstractShape s; // Error: abstract class
Circle c(5.0);
std::cout << c.area() << std::endl; // 78.5397
return 0;
}Question 5
What is an abstract class in C++?
Question 6
What is the difference between static binding and dynamic binding?
class Base {
public:
void nonVirtual() {
std::cout << "Base non-virtual" << std::endl;
}
virtual void isVirtual() {
std::cout << "Base virtual" << std::endl;
}
};
class Derived : public Base {
public:
void nonVirtual() {
std::cout << "Derived non-virtual" << std::endl;
}
void isVirtual() override {
std::cout << "Derived virtual" << std::endl;
}
};
int main() {
Base* b = new Derived();
b->nonVirtual(); // Static binding: calls Base::nonVirtual()
b->isVirtual(); // Dynamic binding: calls Derived::isVirtual()
delete b;
return 0;
}Question 7
What is the importance of virtual destructors in polymorphism?
class Base {
public:
Base() { std::cout << "Base created" << std::endl; }
virtual ~Base() { std::cout << "Base destroyed" << std::endl; } // Virtual!
virtual void doSomething() = 0;
};
class Derived : public Base {
public:
Derived() { std::cout << "Derived created" << std::endl; }
~Derived() { std::cout << "Derived destroyed" << std::endl; }
void doSomething() override {}
};
int main() {
Base* b = new Derived(); // Polymorphic creation
delete b; // Correctly calls ~Derived() then ~Base()
return 0;
}
// Without virtual destructor:
// delete b; // Only calls ~Base(), ~Derived() never called!
// Memory leak and improper cleanup!Question 8
What is dynamic dispatch in C++?
Question 9
What is the difference between virtual functions and pure virtual functions?
Question 10
What is interface-based design in C++?
// Interface (abstract class)
class IDrawable {
public:
virtual void draw() const = 0;
virtual ~IDrawable() = default;
};
class IResizable {
public:
virtual void resize(double factor) = 0;
virtual ~IResizable() = default;
};
// Implementation
class Circle : public IDrawable, public IResizable {
public:
void draw() const override {
std::cout << "Drawing circle" << std::endl;
}
void resize(double factor) override {
radius *= factor;
}
private:
double radius;
};
// Usage through interfaces
void render(const IDrawable& drawable) {
drawable.draw();
}
int main() {
Circle c;
render(c); // Programming to interface
return 0;
}Question 11
What is the virtual function call overhead in C++?
Question 12
What is the difference between early binding and late binding?
Question 13
What is the role of the vptr (virtual pointer) in polymorphism?
Question 14
What is covariant return types in polymorphism?
class Base {
public:
virtual Base* clone() const { return new Base(*this); }
};
class Derived : public Base {
public:
// Covariant return: more specific type than Base*
Derived* clone() const override { return new Derived(*this); }
};
int main() {
Derived d;
Derived* d2 = d.clone(); // Can use Derived* directly
Base* b = d.clone(); // Still works
return 0;
}Question 15
What is the difference between polymorphism and inheritance?
Question 16
What is runtime type identification (RTTI) in polymorphism?
#include <typeinfo>
class Base {
public:
virtual ~Base() = default;
};
class Derived : public Base {};
int main() {
Base* b = new Derived();
// RTTI for type checking
if (typeid(*b) == typeid(Derived)) {
std::cout << "Object is Derived" << std::endl;
}
// Dynamic cast for safe downcasting
Derived* d = dynamic_cast<Derived*>(b);
if (d) {
std::cout << "Cast successful" << std::endl;
}
delete b;
return 0;
}Question 17
What is the difference between static_cast and dynamic_cast in polymorphism?
class Base {
public:
virtual ~Base() = default;
};
class Derived : public Base {
public:
void derivedMethod() {}
};
int main() {
Base* b = new Derived();
// Static cast - no runtime check
Derived* d1 = static_cast<Derived*>(b); // Fast but unsafe
d1->derivedMethod(); // May crash if b doesn't point to Derived
// Dynamic cast - runtime check
Derived* d2 = dynamic_cast<Derived*>(b); // Slower but safe
if (d2) {
d2->derivedMethod(); // Safe to use
}
delete b;
return 0;
}Question 18
What is the virtual destructor problem and how is it solved?
Question 19
What is the difference between polymorphism and overloading?
Question 20
What is the difference between polymorphism and templates?
Question 21
What is the difference between virtual functions and function pointers?
Question 22
What is the difference between polymorphism and duck typing?
Question 23
What is the difference between compile-time polymorphism and runtime polymorphism?
Question 24
What is the difference between polymorphism and generics?
Question 25
What is the difference between virtual methods and abstract methods?
Question 26
What is the difference between polymorphism and encapsulation?
Question 27
What is the difference between polymorphism and abstraction?
Question 28
What is the difference between polymorphism and modularity?
Question 29
What is the difference between polymorphism and composition?
Question 30
What is the difference between polymorphism and parameterization?
Question 31
What is the difference between polymorphism and reflection?
Question 32
What is the difference between polymorphism and metaprogramming?
Question 33
What is the difference between polymorphism and event-driven programming?
Question 34
What is the difference between polymorphism and aspect-oriented programming?
Question 35
What is the difference between polymorphism and functional programming?
Question 36
What is the difference between polymorphism and concurrent programming?
Question 37
What is the difference between polymorphism and distributed programming?
Question 38
What is the difference between polymorphism and reactive programming?
Question 39
What is the difference between polymorphism and declarative programming?
Question 40
What are the fundamental principles for effective polymorphism design in C++?
