C++ OOP: Polymorphism Quiz

C++
0 Passed
0% acceptance

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.

40 Questions
~80 minutes
1

Question 1

What is polymorphism in C++?

A
The ability of objects to exhibit different behaviors through the same interface, achieved through virtual functions and dynamic dispatch at runtime
B
The ability to create multiple objects of the same class
C
The ability to inherit from multiple base classes
D
The ability to overload operators
2

Question 2

What is a virtual function in C++?

cpp
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;
}
A
A member function that can be overridden in derived classes and is resolved at runtime through dynamic dispatch using the vtable mechanism
B
A function that can be called without an object
C
A function that is inherited but cannot be changed
D
A function that is automatically generated by the compiler
3

Question 3

What is the vtable (virtual table) in C++?

A
A compiler-generated table of function pointers used for virtual function dispatch, with each polymorphic class having its own vtable containing addresses of its virtual methods
B
A table of all class member variables
C
A table of inheritance relationships
D
A table of constructor and destructor addresses
4

Question 4

What is a pure virtual function in C++?

cpp
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;
}
A
A virtual function declared with = 0 that has no implementation and makes the class abstract, requiring derived classes to provide concrete implementations
B
A virtual function that cannot be overridden
C
A virtual function that is automatically implemented
D
A virtual function that can only be called statically
5

Question 5

What is an abstract class in C++?

A
A class that contains at least one pure virtual function and cannot be instantiated directly, serving as a base class that defines interfaces for derived classes
B
A class that has no member functions
C
A class that cannot be inherited from
D
A class that has no data members
6

Question 6

What is the difference between static binding and dynamic binding?

cpp
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;
}
A
Static binding resolves calls at compile-time based on pointer/reference type, dynamic binding resolves calls at runtime based on actual object type through vtable lookup
B
They are identical concepts
C
Dynamic binding is faster than static binding
D
Static binding only works with virtual functions
7

Question 7

What is the importance of virtual destructors in polymorphism?

cpp
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!
A
Virtual destructors ensure proper cleanup when deleting derived objects through base class pointers, preventing resource leaks and undefined behavior
B
Virtual destructors make classes polymorphic
C
Virtual destructors prevent inheritance
D
Virtual destructors are automatically generated
8

Question 8

What is dynamic dispatch in C++?

A
The runtime mechanism that selects the appropriate virtual function implementation based on the actual object type, using vtable lookup for polymorphic behavior
B
The process of creating objects at runtime
C
The process of deleting objects dynamically
D
The process of calling non-virtual functions
9

Question 9

What is the difference between virtual functions and pure virtual functions?

A
Virtual functions provide default implementations that can be overridden, pure virtual functions (=0) have no implementation and make the class abstract, requiring overrides
B
They are identical concepts
C
Pure virtual functions are faster than virtual functions
D
Virtual functions cannot be overridden
10

Question 10

What is interface-based design in C++?

cpp
// 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;
}
A
Designing code to depend on abstract interfaces rather than concrete implementations, enabling loose coupling and easier testing through polymorphic behavior
B
Designing classes with many member functions
C
Designing classes that cannot be inherited
D
Designing classes with no data members
11

Question 11

What is the virtual function call overhead in C++?

A
Extra indirection through vtable lookup instead of direct function call, plus vtable pointer storage in each polymorphic object, typically 1-2 extra instructions per call
B
Virtual functions are always slower than non-virtual functions
C
Virtual function calls have no overhead
D
Virtual functions only have overhead in debug builds
12

Question 12

What is the difference between early binding and late binding?

A
Early binding resolves function calls at compile-time, late binding (polymorphism) resolves calls at runtime through virtual dispatch
B
They are identical concepts
C
Late binding is faster than early binding
D
Early binding only works with virtual functions
13

Question 13

What is the role of the vptr (virtual pointer) in polymorphism?

A
A hidden pointer in each polymorphic object that points to the class's vtable, enabling virtual function dispatch by providing the correct function addresses
B
A pointer to the object's data members
C
A pointer to the object's constructor
D
A pointer to the object's destructor
14

Question 14

What is covariant return types in polymorphism?

cpp
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;
}
A
Allowing overridden virtual functions to return more specific types than their base class counterparts, enabling better type safety in polymorphic hierarchies
B
Requiring return types to be identical in base and derived classes
C
Allowing return types to be less specific in derived classes
D
Preventing return type changes in virtual functions
15

Question 15

What is the difference between polymorphism and inheritance?

A
Inheritance enables code reuse through class hierarchies, polymorphism enables runtime behavior variation through virtual dispatch
B
They are identical concepts
C
Polymorphism requires inheritance
D
Inheritance requires polymorphism
16

Question 16

What is runtime type identification (RTTI) in polymorphism?

cpp
#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;
}
A
Language feature providing runtime type information through typeid and dynamic_cast, enabling type checking and safe polymorphic downcasting
B
A way to identify object types at compile-time
C
A way to prevent polymorphism
D
A way to make all functions virtual
17

Question 17

What is the difference between static_cast and dynamic_cast in polymorphism?

cpp
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;
}
A
static_cast performs no runtime checks and can be unsafe, dynamic_cast performs runtime type checking and returns nullptr on failure for safe polymorphic casting
B
They are identical in functionality
C
dynamic_cast is faster than static_cast
D
static_cast only works with polymorphic classes
18

Question 18

What is the virtual destructor problem and how is it solved?

A
Without virtual destructors, deleting derived objects through base pointers only calls base destructor, causing memory leaks; solved by declaring base destructor virtual
B
Virtual destructors prevent inheritance
C
Virtual destructors make all functions virtual
D
Virtual destructors are automatically generated
19

Question 19

What is the difference between polymorphism and overloading?

A
Polymorphism enables runtime method selection through inheritance, overloading enables compile-time method selection through different signatures
B
They are identical concepts
C
Overloading requires inheritance
D
Polymorphism requires overloading
20

Question 20

What is the difference between polymorphism and templates?

A
Polymorphism provides runtime flexibility with inheritance overhead, templates provide compile-time flexibility with code generation but no runtime overhead
B
They are identical approaches
C
Templates are always faster than polymorphism
D
Polymorphism requires templates
21

Question 21

What is the difference between virtual functions and function pointers?

A
Virtual functions provide automatic dispatch through vtables, function pointers require manual management and storage for similar polymorphic behavior
B
They are identical in functionality
C
Function pointers are faster than virtual functions
D
Virtual functions cannot be used for callbacks
22

Question 22

What is the difference between polymorphism and duck typing?

A
Polymorphism requires explicit inheritance relationships, duck typing relies on interface compatibility without inheritance (more flexible but less type-safe)
B
They are identical concepts
C
Duck typing is only used in dynamic languages
D
Polymorphism provides better type safety
23

Question 23

What is the difference between compile-time polymorphism and runtime polymorphism?

A
Compile-time polymorphism (templates/overloading) resolves at compile-time, runtime polymorphism (virtual functions) resolves at runtime with dispatch overhead
B
They are identical concepts
C
Runtime polymorphism is always faster
D
Compile-time polymorphism requires inheritance
24

Question 24

What is the difference between polymorphism and generics?

A
Polymorphism enables runtime behavior variation through inheritance, generics enable compile-time type safety and code reuse through parameterization
B
They are identical concepts
C
Generics are only used in Java and C#
D
Polymorphism requires generics
25

Question 25

What is the difference between virtual methods and abstract methods?

A
Virtual methods provide default implementations that can be overridden, abstract methods (pure virtual) have no implementation and must be overridden
B
They are identical concepts
C
Abstract methods are faster than virtual methods
D
Virtual methods cannot be overridden
26

Question 26

What is the difference between polymorphism and encapsulation?

A
Polymorphism enables flexible behavior through interfaces, encapsulation hides implementation details to protect internal state
B
They are identical concepts
C
Encapsulation requires polymorphism
D
Polymorphism requires encapsulation
27

Question 27

What is the difference between polymorphism and abstraction?

A
Polymorphism enables runtime behavior selection, abstraction hides complexity by focusing on essential characteristics through interfaces
B
They are identical concepts
C
Abstraction requires polymorphism
D
Polymorphism requires abstraction
28

Question 28

What is the difference between polymorphism and modularity?

A
Polymorphism enables flexible component interactions, modularity enables system decomposition into independent, replaceable parts
B
They are identical concepts
C
Modularity requires polymorphism
D
Polymorphism requires modularity
29

Question 29

What is the difference between polymorphism and composition?

A
Polymorphism enables runtime behavior variation through inheritance, composition enables runtime behavior variation through object assembly
B
They are identical concepts
C
Composition requires polymorphism
D
Polymorphism requires composition
30

Question 30

What is the difference between polymorphism and parameterization?

A
Polymorphism enables behavior variation through inheritance hierarchies, parameterization enables behavior variation through configuration and templates
B
They are identical concepts
C
Parameterization requires polymorphism
D
Polymorphism requires parameterization
31

Question 31

What is the difference between polymorphism and reflection?

A
Polymorphism enables compile-time type-safe method dispatch, reflection enables runtime type inspection and manipulation with less type safety
B
They are identical concepts
C
Reflection is only available in dynamic languages
D
Polymorphism requires reflection
32

Question 32

What is the difference between polymorphism and metaprogramming?

A
Polymorphism enables runtime behavior selection, metaprogramming enables compile-time code generation and type manipulation
B
They are identical concepts
C
Metaprogramming requires polymorphism
D
Polymorphism requires metaprogramming
33

Question 33

What is the difference between polymorphism and event-driven programming?

A
Polymorphism enables object behavior variation, event-driven programming enables system behavior variation through message passing and callbacks
B
They are identical concepts
C
Event-driven programming requires polymorphism
D
Polymorphism requires event-driven programming
34

Question 34

What is the difference between polymorphism and aspect-oriented programming?

A
Polymorphism enables behavior variation through inheritance, aspect-oriented programming enables behavior variation through cross-cutting concerns and weaving
B
They are identical concepts
C
Aspect-oriented programming requires polymorphism
D
Polymorphism requires aspect-oriented programming
35

Question 35

What is the difference between polymorphism and functional programming?

A
Polymorphism enables object behavior variation through inheritance, functional programming enables behavior variation through higher-order functions and composition
B
They are identical concepts
C
Functional programming requires polymorphism
D
Polymorphism requires functional programming
36

Question 36

What is the difference between polymorphism and concurrent programming?

A
Polymorphism enables object behavior variation, concurrent programming enables system behavior variation through parallel execution and synchronization
B
They are identical concepts
C
Concurrent programming requires polymorphism
D
Polymorphism requires concurrent programming
37

Question 37

What is the difference between polymorphism and distributed programming?

A
Polymorphism enables local object behavior variation, distributed programming enables system behavior variation through network communication and remote procedure calls
B
They are identical concepts
C
Distributed programming requires polymorphism
D
Polymorphism requires distributed programming
38

Question 38

What is the difference between polymorphism and reactive programming?

A
Polymorphism enables object behavior variation through inheritance, reactive programming enables behavior variation through data flow and event streams
B
They are identical concepts
C
Reactive programming requires polymorphism
D
Polymorphism requires reactive programming
39

Question 39

What is the difference between polymorphism and declarative programming?

A
Polymorphism enables imperative behavior variation through objects, declarative programming enables behavior specification through constraints and rules
B
They are identical concepts
C
Declarative programming requires polymorphism
D
Polymorphism requires declarative programming
40

Question 40

What are the fundamental principles for effective polymorphism design in C++?

A
Use virtual destructors for polymorphic bases, prefer pure virtual interfaces over implementation inheritance, use override/final keywords for clarity, consider performance implications of virtual dispatch, and design for interface compatibility rather than implementation details
B
Make all functions virtual for maximum flexibility
C
Avoid inheritance and use only composition
D
Use multiple inheritance for all polymorphic classes

QUIZZES IN C++