Questions: Ad Hoc Polymorphism and Function Overloading

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

A function `length` works on strings, lists, and arrays using a single uniform implementation that counts elements without inspecting type. A function `add` has one implementation for integers (ALU instruction) and a separate implementation for strings (memory allocation and copying). Which is ad hoc polymorphism?

ABoth `length` and `add` — any function that works across multiple types is ad hoc polymorphic
B`length` only — uniform behavior across types is the defining feature of ad hoc polymorphism
C`add` only — separate implementations per type is the defining feature of ad hoc polymorphism
DNeither — ad hoc polymorphism requires runtime dispatch to be valid
Question 2 Multiple Choice

The compiler encounters `foo(a, b)` where `foo` has overloads for (int, int) and (float, float). Both `a` and `b` are declared as `int`. The language allows implicit int-to-float promotion. How does overload resolution proceed?

AThe call is ambiguous because both overloads could apply after promotion
BThe (int, int) overload is selected as an exact match, with no promotion needed
CThe (float, float) overload is selected because float is the default numeric type
DThe call fails — ad hoc polymorphism does not permit implicit conversions
Question 3 True / False

Overload resolution for ad hoc polymorphism is performed at compile time, not at runtime.

TTrue
FFalse
Question 4 True / False

Ad hoc polymorphism and parametric polymorphism both use a single implementation shared across most applicable types.

TTrue
FFalse
Question 5 Short Answer

Explain why overload resolution can become ambiguous in a language that permits implicit type conversions, and how languages typically handle this.

Think about your answer, then reveal below.