Due: before class on Monday 23 November
Part 1: Type Systems & Polymorphism (Unit 5)
-
In modern programming language terminology, which of the following scenarios best demonstrates “Strong Typing”?
- A language where you must explicitly write
int x = 10;rather than justx = 10;. - 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.
- A language where the type of a variable is determined at compile-time and cannot change.
- A language that allows you to cast a pointer to a specific memory address into an integer and perform arithmetic on it.
- A language where you must explicitly write
-
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?
- The interpreter must perform a “tag check” before every operation (like addition) to ensure the types are compatible.
- Tagged unions require all variables to be stored on the heap, which causes frequent garbage collection pauses.
- The language must generate multiple versions of every function to handle different types.
- It prevents the use of recursion, forcing the programmer to write slower iterative loops.
-
Why is Ad-hoc Polymorphism (Function Overloading) generally impossible to implement in a dynamically typed language like Python?
- Dynamic languages strictly forbid defining two functions with the same name in the same scope.
- The interpreter cannot determine which version of the function to call because the arguments do not have declared types to match against a signature.
- Dynamic languages use vtables for all function calls, which only support single inheritance.
- It would require “Type Erasure,” which is only available in compiled languages like Java.
-
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)- The compiler selects the correct version of the function based only on the declared type (static dispatch)
- The compiler ensures the declared type has a method with that name (static typing)
- 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)
- The JVM runtime will throw an error if the actual object type doesn’t have any method with that name (duck typing)
-
Which of the following statements accurately describes the trade-offs between C++ Templates and Java Generics?
- C++ Templates use “Type Erasure,” resulting in smaller executable files but slower runtime performance due to casting.
- Java Generics use “Monomorphization,” creating specialized copies of the class for every data type used.
- C++ Templates allow for better runtime performance because the compiler generates specialized machine code for each type (e.g.,
vector<int>vsvector<float>). - Java Generics allow you to instantiate new objects of type
T(e.g.,new T()) because the type information is preserved at runtime.
-
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.
-
Translate the
hostGuestfunction to Java using generics. -
Put your function in a file (inside a class) and try to compile it with
javac. What error do you get? -
Explain! Why does this function work with a C++ template but not with Java generics?
-