Virtual Functions and Dynamic Dispatch Quiz Quiz

Deepen your understanding of virtual functions and dynamic dispatch in object-oriented programming. This quiz covers core principles, use cases, and common pitfalls to help you reinforce key concepts related to polymorphism and runtime method resolution.

  1. Identifying Virtual Function Purpose

    Which statement best describes the primary purpose of a virtual function in object-oriented programming?

    1. To increase compilation speed by resolving functions at compile time only
    2. To initialize object data members automatically
    3. To restrict access to base class members from derived classes
    4. To enable derived classes to override base class methods for dynamic behavior at runtime

    Explanation: Virtual functions allow derived classes to provide specific implementations of functions defined in a base class, supporting polymorphism and dynamic dispatch at runtime. Compilation speed is unrelated to virtual functions; if anything, virtual functions slightly decrease compile-time optimization due to runtime resolution. Restricting access is handled by access specifiers, not virtual functions. Data member initialization is managed by constructors, not virtual methods.

  2. Dynamic Dispatch Scenario

    Given a base pointer pointing to a derived object, what happens if a non-virtual member function is called through the base pointer?

    1. A runtime error will occur due to ambiguity
    2. The base class implementation of the function is always invoked
    3. The call will be ignored by the compiler
    4. The derived class implementation will override the call automatically

    Explanation: When calling a non-virtual function through a base pointer, the function call is resolved at compile time, so the base class version is executed regardless of the actual object type. Only virtual functions support dynamic dispatch and allow the derived implementation to be used. There is no ambiguity or runtime error, and the call is not ignored by the compiler.

  3. Virtual Destructor Importance

    Why is it important to declare a base class destructor as virtual when using polymorphism, especially with dynamic memory allocation?

    1. It speeds up the deletion process of objects
    2. It stops the derived class constructor from running twice
    3. It ensures the derived class destructor is called, properly releasing resources
    4. It makes the base class abstract, preventing instantiation

    Explanation: A virtual base class destructor guarantees that when a derived object is deleted via a base pointer, the correct derived class destructor gets called, ensuring proper cleanup. Not marking the destructor virtual can lead to resource leaks if only the base class destructor is executed. Constructors are not affected by the virtual keyword, and making a destructor virtual does not make a class abstract. Virtual destructors may slightly slow down, not speed up, deletion due to dynamic lookup.

  4. Pure Virtual Function Consequence

    What is the effect of declaring a pure virtual function in a class using '= 0'?

    1. All members of the class must now be virtual
    2. The class becomes abstract and cannot be instantiated directly
    3. The function becomes a friend function of all derived classes
    4. The function will be removed from the base class definition

    Explanation: Declaring a pure virtual function makes the class abstract, which means you cannot create objects of that class type directly. The function remains a member of the base class and must be overridden by derived classes, but it's not removed. Not all members are required to be virtual, and pure virtual status does not make it a friend function.

  5. Understanding vtable Usage

    Which role does the vtable play in supporting dynamic dispatch for virtual functions?

    1. It controls access permissions for private data members
    2. It is used solely during compile-time to optimize function calls
    3. It stores object data member values for each class instance
    4. It provides a runtime lookup table allowing selection of the correct overridden function

    Explanation: The vtable is a mechanism that allows the program to select the correct function implementation at runtime when a virtual call occurs, enabling dynamic dispatch. It does not store actual data member values, as those are part of the object instance. The vtable is critical at runtime, not just compile-time. Access permissions are defined using keywords, not by the vtable.