Questions: Subtyping and Type Bounds

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

A function type `(Animal) → Dog` and a function type `(Dog) → Animal` are both in scope. Under correct subtyping rules with contravariant parameters and covariant returns, which is a subtype of which?

A`(Dog) → Animal` is a subtype of `(Animal) → Dog`, because it accepts a narrower input and returns a broader output
B`(Animal) → Dog` is a subtype of `(Dog) → Animal`, because it accepts a broader input and returns a narrower output
CNeither is a subtype of the other; function types are invariant
DBoth are subtypes of each other, since they accept the same types in different directions
Question 2 Multiple Choice

What does an upper bound `T extends Comparable<T>` on a generic type parameter primarily enable?

AIt prevents T from being instantiated with primitive types, improving runtime performance
BIt allows the compiler to guarantee that values of type T have a compareTo method, enabling type-safe generic operations like sorting
CIt restricts T to exactly the Comparable type, removing the benefit of generics
DIt forces callers to provide explicit type annotations rather than relying on inference
Question 3 True / False

If `Dog` is a subclass of `Animal`, then `List<Dog>` is a subtype of `List<Animal>` in a type-safe generic system.

TTrue
FFalse
Question 4 True / False

In a structural type system, a type can be a subtype of another type even without any explicit declaration of inheritance or interface implementation.

TTrue
FFalse
Question 5 Short Answer

Why must function types be contravariant in their parameter types? Give a concrete example showing what goes wrong if parameter types were covariant instead.

Think about your answer, then reveal below.