Variance Flashcards

1
Q

What does the concept of “variance” refer to in object-oriented programming?

  1. The ability to execute multiple methods on an object
  2. The substitutability of types in generics and delegates
  3. The inheritance hierarchy of an object
  4. The size and structure of collections
A
  1. The substitutability of types in generics and delegates

(Förklaring: Variance describes how type substitutability is handled, particularly in relation to generics and delegates).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What type of variance allows a derived type to be used as a more general type?

  1. Contravariance
  2. Invariance
  3. Bivariance
  4. Covariance
A
  1. Covariance

(Förklaring: Covariance allows a derived (more specific) type to be used in place of a base (more general) type).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

In C#, what is required for a type parameter to be covariant?

  1. It must use the in keyword
  2. It must use the out keyword
  3. It must be a value type
  4. It must implement a delegate
A
  1. 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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which of these statements about covariance is correct?

  1. Covariance only applies to input parameters
  2. Covariance can only be used with classes
  3. Covariance applies to return types
  4. Covariance allows a more general type to substitute for a more specific type
A
  1. Covariance applies to return types

(Förklaring: Covariance allows a function to return a more specific type than expected by the delegate or interface).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

In which scenario is covariance commonly used?

  1. When using a generic collection for input parameters
  2. When converting between classes in an inheritance hierarchy
  3. When assigning a more specific delegate return type to a general delegate
  4. When overriding a method with a broader parameter type
A
  1. 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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is contravariance in the context of generics?

  1. Allowing a derived type to be used in place of a base type
  2. Allowing a base type to be used in place of a derived type
  3. Prohibiting any type substitution
  4. Permitting type substitution only for collections
A
  1. 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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In C#, what keyword is used to denote contravariance in a generic type parameter?

  1. in
  2. out
  3. base
  4. covariant
A
  1. in

(Förklaring: The in keyword specifies that a type parameter is contravariant, allowing more general types to be substituted).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Contravariance in C# is typically used with which of the following?

  1. Return types
  2. Input parameters
  3. Value types only
  4. Interfaces without generics
A
  1. Input parameters

(Förklaring: Contravariance is applied to input parameters, enabling flexibility in method and delegate assignments).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does “invariance” mean in the context of generics?

  1. Types can be freely substituted regardless of hierarchy
  2. Types cannot be substituted with different types
  3. Types can only be used in output positions
  4. Types can only be used in input positions
A
  1. 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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why is IList<T> considered invariant in C#?</T>

  1. It only accepts input types
  2. It only returns output types
  3. It allows both input and output, preventing variance
  4. It is not generic
A
  1. 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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

In C#, which of these delegates allows contravariance?

  1. Func<out></out>
  2. Func<in T, TResult>
  3. Action<in></in>
  4. Predicate<T></T>
A
  1. 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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What rule does the Liskov Substitution Principle (LSP) enforce regarding variance?

  1. Covariance can be applied to all types
  2. A subtype should be usable wherever a supertype is expected
  3. Contravariance applies only to method return types
  4. Variance should only be applied to immutable collections
A
  1. 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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

In C#, what is an example of a covariant type in generics?

  1. IList<T></T>
  2. Action<T></T>
  3. IEnumerable<T></T>
  4. Dictionary<T></T>
A
  1. 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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What will happen if you attempt to assign an IList<Apple> to an IList<Fruit> variable?</Fruit></Apple>

  1. It will compile successfully
  2. It will throw an exception at runtime
  3. It will cause a compile-time error
  4. It will result in an invariant error at runtime
A
  1. 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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the main difference between covariance and contravariance in delegates?

  1. Covariance applies to return types, contravariance to input parameters
  2. Contravariance applies to return types, covariance to input parameters
  3. Both apply only to interfaces
  4. Only contravariance applies to generic types
A
  1. 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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly