GPT 4 tentafrågor - Collections & collection initializers Flashcards

1
Q

Varför föredrar utvecklare ofta att använda generiska samlingar över traditionella
arrayer i C#?

A. Generiska samlingar tillåter dynamisk storleksändring.
B. Generiska samlingar kräver mindre kod för initialisering.
C. Generiska samlingar stöder endast primitiva datatyper.

A

Rätt svar: A. Generiska samlingar tillåter dynamisk storleksändring.

Förklaring: Generiska samlingar, som List<T> och Dictionary<TKey, TValue>, erbjuder mer flexibilitet än traditionella arrayer, inklusive förmågan att dynamiskt ändra storlek och enklare metoder för att lägga till och ta bort element.</T>

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

Fråga 2: Vilken metod skulle du använda för att avgöra om ett specifikt element finns inom en instance av ICollection<T>?
A. Find(T item)
B. Contains(T item)
C. Retrieve(T item)</T>

A

Rätt svar: B. Contains(T item)

Förklaring: Contains(T item)-metoden är en standardmetod tillhandahållen av ICollection<T>-gränssnittet för att kontrollera om ett element finns i samlingen.</T>

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

Fråga 3: Vilket påstående är sant om ICollection<T>-gränssnittet?
A. Det tillåter inte modifiering av element efter att de har lagts till.
B. Det definierar ett kontrakt för samlingar som kan ändras efter skapande.
C. Det stöder endast läsoperationer på samlingar.</T>

A

Rätt svar: B. Det definierar ett kontrakt för samlingar som kan ändras efter skapande.

Förklaring: ICollection<T> är grundläggande för generiska samlingar som är föränderliga (dvs. kan modifieras efter skapande), vilket innebär att man kan lägga till, ta bort, eller söka efter element i dessa samlingar.</T>

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

Betrakta följande kodsnutt. Vad kommer att skrivas ut till konsolen?

csharp

var numbers = new List<int>();
numbers.Add(10);
numbers.Add(20);
numbers.Remove(10);
Console.WriteLine(numbers.Count);

A. 0
B. 1
C. 2

A

Rätt svar: B. 1

Förklaring: Listan innehåller ursprungligen två element, “10” och “20”. Efter att “10” tas bort, innehåller listan endast ett element, vilket resulterar i att “1” skrivs ut till konsolen.

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

Fråga 5: Vilken klass skulle vara mest lämplig för att lagra ett dynamiskt antal objekt av en specifik typ, med möjligheten att enkelt lägga till och ta bort objekt?

A. Array
B. List<T>
C. ICollection<T>


A

B:

Förklaring: List<T> är en klass som implementerar ICollection<T> och erbjuder funktionalitet för att enkelt lägga till och ta bort objekt, samt hantera ett dynamiskt antal objekt av en specifik typ.</T></T>

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

What is the primary purpose of collection initializers in C#?

a) To create dynamic collections that automatically adjust size.
b) To offer a faster execution time for programs utilizing collections.
c) To allow for more concise and readable code during the collection population process.

A

Correct answer: c

Explanation: Collection initializers simplify the process of adding items to a collection at the time of its creation. They enable programmers to initialize a collection and populate it with an initial set of values in a single, clear, and concise statement. This approach enhances code readability and cleanliness by reducing the need for multiple lines of Add method calls.

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

How do collection initializers work for lists in C#?

a) They require a special method called Initialize to add elements.
b) They allow you to declare and populate a list of elements within a single statement, using curly braces {}.
c) They use a Set method to assign all values simultaneously.

A

Correct answer: b) They allow you to declare and populate a list of elements within a single statement, using curly braces {}.

Explanation: In C#, collection initializers let you instantiate a list (or other collection types) and simultaneously populate it with elements. This is done within a set of curly braces {} directly following the instantiation, where each element is specified, separated by commas. It eliminates the need for separate Add calls for each item, thereby making the code more concise and readable.

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

Which of the following statements is true regarding collection initializers?

a. They can only be used at the creation of a collection; to add more elements later, you must use methods like Add.

b. They allow elements to be added or removed from a collection at any time in the code without the use of other methods.

c) They create a deep copy of the collection each time you add new elements, ensuring thread safety.

A

Correct answer: a) They can only be used at the creation of a collection; to add more elements later, you must use methods like Add.

Explanation: Collection initializers are a syntactic convenience used at the point of creation of the collection. They’re used to populate new collections with initial elements seamlessly. If you want to add more elements after the collection has been created, you need to use the collection’s methods (like Add) because collection initializers don’t apply after the initial declaration and population.

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

Consider the following code segment. What is wrong with this way of using collection initializers?

var numbers = new List<int> { 1, 2, 3, 4 };
numbers = new List<int> { 5, 6, 7, 8 }; // What's the issue here?

a) You cannot reinitialize a list with new values after the first declaration.
b) C# does not allow you to use collection initializers more than once in the same code block.
c) There is no direct error with the code, but it creates a new instance of the list instead of adding new elements to the existing collection.

A

Correct answer: c) There is no direct error with the code, but it creates a new instance of the list instead of adding new elements to the existing collection.

Explanation: The code is syntactically correct, but it may lead to confusion or unintended consequences. The second statement doesn’t add elements to the first list; instead, it creates an entirely new list and assigns it to the numbers variable. This action overwrites the original list, and the initial elements (1, 2, 3, 4) are no longer accessible through the numbers variable.

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

In which situation is it most appropriate to use collection initializers?

a) When you have a large collection that needs to be populated dynamically at runtime.
b) When you want to optimize memory usage by limiting the number of objects created.
c) When creating and populating a collection with a known set of elements for readability and concise code.

A

Correct answer: c) When creating and populating a collection with a known set of elements for readability and concise code.

Explanation: Collection initializers are most beneficial when you’re working with a static set of known elements that you want to add to a collection at the time of its instantiation. They help in making the code cleaner and more readable by consolidating what would otherwise be multiple lines of element addition into a single, concise statement. However, for dynamic or large collections where elements are determined at runtime, traditional methods like Add are more appropriate.

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