Polymorphism Made Simple: Compile-Time vs Runtime Quiz

Deepen your understanding of polymorphism in object-oriented programming by exploring the distinctions between compile-time and runtime mechanisms. This quiz assesses your grasp of key concepts, practical examples, and terminology related to how polymorphism operates in different contexts.

  1. Spotting Compile-Time Polymorphism

    Which example best demonstrates compile-time polymorphism in a programming language?

    1. A method named add() that is overloaded to accept both two and three parameters.
    2. A parent class reference holding a subclass object and invoking an overridden method at runtime.
    3. A subclass method replacing a non-static method from its superclass.
    4. A dynamic type check performed using the instanceof operator.

    Explanation: Compile-time polymorphism occurs when method or operator calls are resolved during compilation, such as with method overloading, where methods share the same name but differ in parameter types or count. In this example, 'add()' is overloaded, so the correct method is chosen at compile time. The parent class reference holding a subclass object and invoking an overridden method describes runtime polymorphism. Type checking with instanceof and method overriding also relate to runtime, not compile-time, polymorphism.

  2. Core Concept of Runtime Polymorphism

    Which characteristic is essential for achieving runtime polymorphism in object-oriented programming?

    1. Static binding of methods.
    2. Unique function signatures for each class.
    3. Method overriding in a subclass.
    4. Function pointers without inheritance.

    Explanation: Runtime polymorphism relies on method overriding, where a subclass provides a specific implementation for a method declared in its superclass, allowing behavior to be determined at runtime. Static binding is associated with compile-time polymorphism, while unique function signatures (no overriding) prevent polymorphism. Function pointers can be used for dynamic behavior, but without inheritance, runtime polymorphism within object-oriented programming does not occur.

  3. Terminology Check: Binding Time

    What does the term 'late binding' refer to in the context of polymorphism?

    1. Modifying a function's behavior with static variables.
    2. Associating a method call with its body at runtime.
    3. Resolving a variable's data type during compilation.
    4. Declaring multiple constructors for a class.

    Explanation: Late binding means that the method to execute is determined at runtime based on the actual object's type, which is the essence of runtime polymorphism. Resolving types during compilation is called early binding. Multiple constructors relate to overloading, not binding. Static variables have no direct relation to binding time; they only affect variable lifetime, not dynamic dispatch.

  4. Understanding Limitations

    Which feature does NOT enable compile-time polymorphism?

    1. Function overloading with different parameter lists.
    2. Inheritance and method overriding.
    3. Operator overloading for custom types.
    4. Templates or generics in statically typed languages.

    Explanation: Inheritance with method overriding enables runtime polymorphism since the method to invoke is determined at runtime. Operator and function overloading are examples of compile-time polymorphism, resolved during compilation. Templates and generics also provide compile-time polymorphism by allowing code reusability and type flexibility, with type resolution occurring before execution.

  5. Applying the Principle

    Given an array of references to a superclass, calling an overridden method on each element at runtime best exemplifies which concept?

    1. Static function resolution.
    2. Dynamic method dispatch.
    3. Method overloading.
    4. Instantiation error handling.

    Explanation: Dynamic method dispatch is the mechanism used in runtime polymorphism, where the method that executes depends on the object's actual type during execution. Static function resolution relates to compile-time polymorphism. Method overloading refers to functions with the same name but different parameters, a compile-time concept. Instantiation error handling is unrelated to polymorphism and involves object creation rather than method selection.