Due: before class on Monday 23 November

Part 1: Type Systems & Polymorphism (Unit 5)

  1. In modern programming language terminology, which of the following scenarios best demonstrates “Strong Typing”?

    1. A language where you must explicitly write int x = 10; rather than just x = 10;.
    2. A language that throws a runtime error if you try to multiply a string by a boolean, rather than implicitly converting the boolean to 0 or 1.
    3. A language where the type of a variable is determined at compile-time and cannot change.
    4. A language that allows you to cast a pointer to a specific memory address into an integer and perform arithmetic on it.
  2. Consider the implementation of Dynamic Typing using a “tagged union” approach (as seen in Python or Scheme). Why does this implementation inherently introduce runtime overhead compared to static typing?

    1. The interpreter must perform a “tag check” before every operation (like addition) to ensure the types are compatible.
    2. Tagged unions require all variables to be stored on the heap, which causes frequent garbage collection pauses.
    3. The language must generate multiple versions of every function to handle different types.
    4. It prevents the use of recursion, forcing the programmer to write slower iterative loops.
  3. Why is Ad-hoc Polymorphism (Function Overloading) generally impossible to implement in a dynamically typed language like Python?

    1. Dynamic languages strictly forbid defining two functions with the same name in the same scope.
    2. The interpreter cannot determine which version of the function to call because the arguments do not have declared types to match against a signature.
    3. Dynamic languages use vtables for all function calls, which only support single inheritance.
    4. It would require “Type Erasure,” which is only available in compiled languages like Java.
  4. In Java when we write a method call on an object like midn.march(), what happens at compile-time and at run-time? (Select all that apply)

    1. The compiler selects the correct version of the function based only on the declared type (static dispatch)
    2. The compiler ensures the declared type has a method with that name (static typing)
    3. The JVM runtime looks up which version of the function to call based on the actual type of the object using a vtable (dynamic dispatch)
    4. The JVM runtime will throw an error if the actual object type doesn’t have any method with that name (duck typing)
  5. Which of the following statements accurately describes the trade-offs between C++ Templates and Java Generics?

    1. C++ Templates use “Type Erasure,” resulting in smaller executable files but slower runtime performance due to casting.
    2. Java Generics use “Monomorphization,” creating specialized copies of the class for every data type used.
    3. C++ Templates allow for better runtime performance because the compiler generates specialized machine code for each type (e.g., vector<int> vs vector<float>).
    4. Java Generics allow you to instantiate new objects of type T (e.g., new T()) because the type information is preserved at runtime.
  6. Consider the following C++ program:

    #include <iostream>
    #include <string>
    
    struct Uncle {
        std::string name;
        Uncle(std::string n) : name(n) {}
        void eatTurkey()     { std::cout << name << " eats three plates of turkey.\n"; }
        void watchFootball() { std::cout << name << " yells at the referee.\n"; }
        void sleep()         { std::cout << name << " snores in the recliner.\n"; }
    };
    
    struct Dog {
        std::string name;
        Dog(std::string n) : name(n) {}
        void eatTurkey()     { std::cout << name << " steals turkey off the counter.\n"; }
        void watchFootball() { std::cout << name << " barks at the mascot.\n"; }
        void sleep()         { std::cout << name << " naps under the table.\n"; }
    };
    
    template <typename T>
    T hostGuest(std::string name) {
        T guest(name);   // Constructs and returns a T
        guest.eatTurkey();
        guest.watchFootball();
        guest.sleep();
        return guest;
    }
    
    int main() {
        Uncle bob = hostGuest<Uncle>("Uncle Bob");
        Dog barnaby = hostGuest<Dog>("Barnaby");
        std::cout << "Survivors: " << bob.name << " & " << barnaby.name << "\n";
    }

    Compile this code and make sure it works as you would expect.

    1. Translate the hostGuest function to Java using generics.

    2. Put your function in a file (inside a class) and try to compile it with javac. What error do you get?

    3. Explain! Why does this function work with a C++ template but not with Java generics?