Variance Flashcards
What does the concept of “variance” refer to in object-oriented programming?
- The ability to execute multiple methods on an object
- The substitutability of types in generics and delegates
- The inheritance hierarchy of an object
- The size and structure of collections
- The substitutability of types in generics and delegates
(Förklaring: Variance describes how type substitutability is handled, particularly in relation to generics and delegates).
What type of variance allows a derived type to be used as a more general type?
- Contravariance
- Invariance
- Bivariance
- Covariance
- Covariance
(Förklaring: Covariance allows a derived (more specific) type to be used in place of a base (more general) type).
In C#, what is required for a type parameter to be covariant?
- It must use the in keyword
- It must use the out keyword
- It must be a value type
- It must implement a delegate
- It must use the out keyword
(Förklaring: The out keyword indicates that a type parameter is covariant, allowing it to be used as a more general type).
Which of these statements about covariance is correct?
- Covariance only applies to input parameters
- Covariance can only be used with classes
- Covariance applies to return types
- Covariance allows a more general type to substitute for a more specific type
- Covariance applies to return types
(Förklaring: Covariance allows a function to return a more specific type than expected by the delegate or interface).
In which scenario is covariance commonly used?
- When using a generic collection for input parameters
- When converting between classes in an inheritance hierarchy
- When assigning a more specific delegate return type to a general delegate
- When overriding a method with a broader parameter type
- When assigning a more specific delegate return type to a general delegate
(Förklaring: Covariance allows methods with more specific return types to be used with delegates that have general return types).
What is contravariance in the context of generics?
- Allowing a derived type to be used in place of a base type
- Allowing a base type to be used in place of a derived type
- Prohibiting any type substitution
- Permitting type substitution only for collections
- Allowing a base type to be used in place of a derived type
(Förklaring: Contravariance enables a more general type to be used where a more specific type is expected, often in input parameters).
In C#, what keyword is used to denote contravariance in a generic type parameter?
- in
- out
- base
- covariant
- in
(Förklaring: The in keyword specifies that a type parameter is contravariant, allowing more general types to be substituted).
Contravariance in C# is typically used with which of the following?
- Return types
- Input parameters
- Value types only
- Interfaces without generics
- Input parameters
(Förklaring: Contravariance is applied to input parameters, enabling flexibility in method and delegate assignments).
What does “invariance” mean in the context of generics?
- Types can be freely substituted regardless of hierarchy
- Types cannot be substituted with different types
- Types can only be used in output positions
- Types can only be used in input positions
- Types cannot be substituted with different types
(Förklaring: Invariance means that a type parameter must match exactly and cannot be substituted with other types).
Why is IList<T> considered invariant in C#?</T>
- It only accepts input types
- It only returns output types
- It allows both input and output, preventing variance
- It is not generic
- It allows both input and output, preventing variance
(Förklaring: Because IList<T> permits both input and output, it must remain invariant to maintain type safety).</T>
In C#, which of these delegates allows contravariance?
- Func<out></out>
- Func<in T, TResult>
- Action<in></in>
- Predicate<T></T>
- Action<in></in>
(Förklaring: Action<in> is contravariant in T, allowing a more general type to be passed to a delegate expecting a specific type).</in>
What rule does the Liskov Substitution Principle (LSP) enforce regarding variance?
- Covariance can be applied to all types
- A subtype should be usable wherever a supertype is expected
- Contravariance applies only to method return types
- Variance should only be applied to immutable collections
- A subtype should be usable wherever a supertype is expected
(Förklaring: LSP states that subtypes must be substitutable for supertypes, which affects how variance is applied safely).
In C#, what is an example of a covariant type in generics?
- IList<T></T>
- Action<T></T>
- IEnumerable<T></T>
- Dictionary<T></T>
- IEnumerable<T></T>
(Förklaring: IEnumerable<T> is covariant, meaning IEnumerable<Apple> can be assigned to IEnumerable<Fruit> if Apple is a subtype of Fruit).</Fruit></Apple></T>
What will happen if you attempt to assign an IList<Apple> to an IList<Fruit> variable?</Fruit></Apple>
- It will compile successfully
- It will throw an exception at runtime
- It will cause a compile-time error
- It will result in an invariant error at runtime
- It will cause a compile-time error
(Förklaring: Since IList<T> is invariant, an exact match in type is required, preventing IList<Apple> from being assigned to IList<Fruit>).</Fruit></Apple></T>
What is the main difference between covariance and contravariance in delegates?
- Covariance applies to return types, contravariance to input parameters
- Contravariance applies to return types, covariance to input parameters
- Both apply only to interfaces
- Only contravariance applies to generic types
- Covariance applies to return types, contravariance to input parameters
(Förklaring: Covariance applies when a delegate’s return type is more specific, while contravariance applies when input parameters are more general).