Generics Flashcards

En del cards är fucked, datorn lägger till vissa saker själv

1
Q

What is a “generic” in programming?

  1. A fixed data type
  2. A way to duplicate code
  3. A feature that allows code to work with any data type
  4. A method for run-time error checking
A
  1. A feature that allows code to work with any data type.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does “parametric polymorphism” refer to in generics?

  1. Using specific types in all functions
  2. Allowing code to work with multiple data types
  3. Type casting at runtime
  4. Only using integer types
A
  1. Allowing code to work with multiple data types
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why are generics useful for collections?

  1. They avoid the need for casting
  2. They allow faster computation
  3. They use less memory
  4. They require fewer methods
A
  1. They avoid the need for casting
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which of the following is a valid generic list declaration for integers in C#?

  1. List<string> myList = new List<string>();</string></string>
  2. List<int> myList = new List<int>();</int></int>
  3. Array<int> myList = new Array<int>();</int></int>
  4. var myList = List<int>();</int>

Det som är efter “;” är en bug. Går ej att få bort

A
  1. List<int> myList = new List<int>();</int></int>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a type parameter in a generic?

  1. A method parameter
  2. A placeholder for a specific data type
  3. A return type
  4. A variable in the generic function
A
  1. A placeholder for a specific data type

(Explanation: List<int> intList = new List<int>();
In this example, T in List<T> is the type parameter, which is replaced with int when creating intList. This allows List to work with any type, not just integers).</T></int></int>

Bug efter “).”

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

Fill in the blank: Generics maintain ____.

  1. dynamic type safety
  2. runtime type safety
  3. static type safety
  4. none of the above
A
  1. static type safety
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which keyword in C# is used for generic constraints?

  1. extends
  2. implement
  3. where
  4. with
A
  1. where

(Explanation: Generic constraints restrict the types that can be used as arguments for type parameters. For example, if you want to ensure that a type parameter T implements a particular interface, you can use the where keyword in C#. Here’s an example:

public class Repository<T> where T : IEntity
{
// T must implement IEntity interface
}</T>

In this case, T must be a type that implements the IEntity interface, so you can’t use a type that does not meet this constraint).

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

What will this code output?
List<int> numbers = new List<int> { 1, 2, 3 };
foreach (var num in numbers)
Console.Write(num);</int></int>

  1. 1 2 3
  2. 1, 2, 3
  3. 1; 2; 3
  4. 123
A
  1. 123
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What type would replace <T> in List<T> if creating a list of strings?</T></T>

  1. int
  2. object
  3. string
  4. float
A
  1. string
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What would be the output of this code snippet?
csharp Pair<int, string> pair = new Pair<int, string>(10, “Apples”);
Console.WriteLine(pair);

  1. (10, Apples)
  2. (10 Apples)
  3. (int, string)
  4. (Pair)
A
  1. (10, Apples)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

True or False: Every type can be converted into a generic type.

  1. True
  2. False
A
  1. False

(Explanation: Not all types can be made generic because some types rely on very specific, concrete details of other types that generics are not designed to accommodate. For example, certain types in C# are constrained by fixed behavior or requirements that don’t make sense in a generic context, like those that need specific numeric or primitive operation).

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

Which of the following statements correctly defines a generic class in C#?

  1. class MyClass<T> { }</T>
  2. class MyClass<> { }
  3. generic class MyClass { }
  4. MyClass<T> { class }</T>
A
  1. class MyClass<T> { }</T>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

In which case would a generic method with <T> be useful?</T>

  1. When all methods need to work only with strings
  2. When the same logic applies to multiple data types
  3. When only integer operations are needed
  4. When casting is required frequently
A
  1. When the same logic applies to multiple data types
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What will this code snippet output?
csharp List<string> words = new List<string>(); words.Add("Hello");
words.Add("World");
foreach (var word in words)
Console.Write(word);</string></string>

  1. HelloWorld
  2. Hello, World
  3. World Hello
  4. Hello; World
A
  1. HelloWorld
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the primary benefit of generics over using object type in collections?

  1. More memory efficient
  2. Ensures type safety at compile-time
  3. Allows dynamic typing
  4. Simplifies code structure
A
  1. Ensures type safety at compile-time
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Which syntax is used to add a constraint on a type parameter?

  1. class MyClass<T : constraint> { }
  2. class MyClass<T> { }</T>
  3. class MyClass(constraint T) { }
  4. class MyClass constraint T { }
A
  1. class MyClass<T : constraint> { }
17
Q

Given Pair<T1, T2>, what does T1 and T2 represent?

  1. Method names
  2. Actual types for the instance
  3. Generic type parameters
  4. Base class names
A
  1. Generic type parameters
18
Q

Which of the following describes a constructed generic type?

  1. Pair<T1, T2>
  2. Pair<int, string>
  3. List<>
  4. object
A
  1. Pair<int, string>

(Explanation: A constructed generic type is a generic type where the “type parameters” have been specified with actual types. For instance, List<int> is a constructed generic type because T has been replaced with int. int is called the "type argument". By contrast, List<T> is a generic type definition with T as a placeholder for an actual type that will be provided when the list is created).</T></int>

19
Q

What happens if you try to add an integer to a List<string>?</string>

  1. Compile-time error
  2. No error
  3. Run-time error
  4. Warning only
A
  1. Compile-time error
20
Q

What is the purpose of type inference in generics?

  1. To automatically determine the type at runtime
  2. To simplify the syntax by determining the type automatically
  3. To allow explicit typing in all instances
  4. To provide optional typing for variables
A
  1. To simplify the syntax by determining the type automatically

(Explanation: Type inference in generics allows the compiler to automatically determine the type parameter based on the arguments provided, without requiring you to explicitly specify the type. This simplifies the syntax and improves readability. For example:

var numbers = new List<int> { 1, 2, 3 };</int>

Here, C# automatically infers that numbers is a List<int> based on the type of elements inside the list, making the code shorter and clearer).</int>

21
Q

What is covariance in generics?

  1. Allowing assignment to a more specific type
  2. Allowing assignment to a more general type
  3. Allowing assignment of numeric types only
  4. Allowing any type to be assigned
A
  1. Allowing assignment to a more general type

(Explanation: Covariance allows you to assign a more specific type to a more general type. For example, you can assign IEnumerable<string> to IEnumerable<object> because string is a more specific type than object. Covariance is commonly used in interfaces and delegates where this flexibility can avoid additional casting.</object></string>

IEnumerable<string> strings = new List<string> { "hello", "world" };
IEnumerable<object> objects = strings; // Covariance allows this assignment</object></string></string>

).

22
Q

In which scenario would covariance be useful?

  1. When assigning List<int> to List<double></double></int>
  2. When assigning IEnumerable<string> to Enumerable<object></object></string>
  3. When assigning object to string
  4. When assigning List<int> to List<object></object></int>
A
  1. When assigning IEnumerable<string> to Enumerable<object></object></string>

(Explanation: Covariance is particularly useful in scenarios where you want to work with a collection of objects in a general way, even if the collection originally contained more specific types. For example, if you want to pass a List<string> to a method that accepts IEnumerable<object>, covariance allows this without requiring a cast or new list.</object></string>

void PrintObjects(IEnumerable<object> items)
{
foreach (var item in items)
{
Console.WriteLine(item);
}
}</object>

IEnumerable<string> words = new List<string> { "apple", "banana" };
PrintObjects(words); // Covariance allows this to work</string></string>

).

23
Q

What is contravariance in generics?

  1. Allowing assignment from a more specific type to a general type
  2. Allowing assignment from a more general type to a more specific type
  3. Allowing two generic types to be assigned freely
  4. Allowing reference types to be assigned as value types
A
  1. Allowing assignment from a more general type to a more specific type

(Explanation: Contravariance is the opposite of covariance; it allows assigning a more general type to a more specific type. This is primarily used in generic delegates where you might want a method that accepts a broader type to handle a narrower one. Contravariance is often seen in event handlers or situations where an action applies to a base class but can accept derived classes.

Action<object> printObject = obj => Console.WriteLine(obj);
Action<string> printString = printObject; // Contravariance allows this assignment
printString("Hello"); // Works fine, even though printObject expects object</string></object>

).

24
Q

Which constraint enforces that a type parameter must have a parameterless constructor?

  1. where T : struct
  2. where T : class
  3. where T : new()
  4. where T : static
A
  1. where T : new()

(Explanation: The new() constraint ensures that a type parameter has a parameterless constructor. This is useful if you need to instantiate the generic type within the class or method. For instance, if you’re creating new instances of T within a method, the compiler needs to know T has a parameterless constructor.

public class Factory<T> where T : new()
{
public T CreateInstance()
{
return new T(); // Valid because of the `new()` constraint
}
}</T>

).

25
Q

How does the .NET runtime handle generics for reference types?

  1. By duplicating code for each type
  2. By resolving types at runtime
  3. By converting all generics to object
  4. By compiling each generic instance separately
A
  1. By resolving types at runtime

(Explanation: The .NET runtime handles generics differently for reference and value types. For reference types, the runtime reuses the same generic code, only substituting the type parameter when necessary. This approach minimizes memory use and ensures that generics maintain type safety without duplicating code.

Example: If you have List<int> and List<string>, the runtime will use the same generic implementation for both lists, only swapping in int and string as needed rather than creating separate code for each. This helps generics stay efficient while still providing type safety).</string></int>