Generics Flashcards
En del cards är fucked, datorn lägger till vissa saker själv
What is a “generic” in programming?
- A fixed data type
- A way to duplicate code
- A feature that allows code to work with any data type
- A method for run-time error checking
- A feature that allows code to work with any data type.
What does “parametric polymorphism” refer to in generics?
- Using specific types in all functions
- Allowing code to work with multiple data types
- Type casting at runtime
- Only using integer types
- Allowing code to work with multiple data types
Why are generics useful for collections?
- They avoid the need for casting
- They allow faster computation
- They use less memory
- They require fewer methods
- They avoid the need for casting
Which of the following is a valid generic list declaration for integers in C#?
- List<string> myList = new List<string>();</string></string>
- List<int> myList = new List<int>();</int></int>
- Array<int> myList = new Array<int>();</int></int>
- var myList = List<int>();</int>
Det som är efter “;” är en bug. Går ej att få bort
- List<int> myList = new List<int>();</int></int>
What is a type parameter in a generic?
- A method parameter
- A placeholder for a specific data type
- A return type
- A variable in the generic function
- 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 “).”
Fill in the blank: Generics maintain ____.
- dynamic type safety
- runtime type safety
- static type safety
- none of the above
- static type safety
Which keyword in C# is used for generic constraints?
- extends
- implement
- where
- with
- 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).
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 2 3
- 1, 2, 3
- 1; 2; 3
- 123
- 123
What type would replace <T> in List<T> if creating a list of strings?</T></T>
- int
- object
- string
- float
- string
What would be the output of this code snippet?
csharp Pair<int, string> pair = new Pair<int, string>(10, “Apples”);
Console.WriteLine(pair);
- (10, Apples)
- (10 Apples)
- (int, string)
- (Pair)
- (10, Apples)
True or False: Every type can be converted into a generic type.
- True
- False
- 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).
Which of the following statements correctly defines a generic class in C#?
- class MyClass<T> { }</T>
- class MyClass<> { }
- generic class MyClass { }
- MyClass<T> { class }</T>
- class MyClass<T> { }</T>
In which case would a generic method with <T> be useful?</T>
- When all methods need to work only with strings
- When the same logic applies to multiple data types
- When only integer operations are needed
- When casting is required frequently
- When the same logic applies to multiple data types
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>
- HelloWorld
- Hello, World
- World Hello
- Hello; World
- HelloWorld
What is the primary benefit of generics over using object type in collections?
- More memory efficient
- Ensures type safety at compile-time
- Allows dynamic typing
- Simplifies code structure
- Ensures type safety at compile-time