Iterators Flashcards

1
Q

What is the primary purpose of the “Iterator pattern”?

  1. To directly modify elements within a collection
  2. To access elements of a collection without exposing its underlying structure
  3. To sort elements in a collection
  4. To link multiple collections together
A
  1. To access elements of a collection without exposing its underlying structure

(Förklaring: Iterator-mönstret gör att vi kan komma åt elementen i en samling utan att exponera dess underliggande struktur, vilket skapar en lös koppling).

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

What does an iterator allow a client to do?

  1. Access an aggregate object’s elements sequentially without knowing its internal structure
  2. Modify an aggregate object’s elements
  3. Randomly access any element in an aggregate object
  4. Directly control an aggregate object’s internal representation
A
  1. Access an aggregate object’s elements sequentially without knowing its internal structure

(Förklaring: Iteratorn tillåter sekventiell åtkomst till en samlings element utan att klienten behöver känna till samlingens interna struktur).

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

In a typical Iterator pattern, what method advances to the next element in the sequence?

  1. Advance()
  2. MoveNext()
  3. NextElement()
  4. FetchNext()
A
  1. MoveNext()

(Förklaring: MoveNext() är metoden i C#-iteratorer som går vidare till nästa element i samlingen).

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

Which interface in C# represents an iterator that can be used to traverse a collection?

  1. ICollection
  2. IAggregate
  3. IEnumerator
  4. IEnumerable
A
  1. IEnumerator

(Förklaring: IEnumerator är gränssnittet i C# som används för att iterera genom en samling element).

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

What is the purpose of the IEnumerable interface in C#?

  1. To provide sorting capabilities for collections
  2. To enable collections to be traversed sequentially
  3. To allow random access to collection elements
  4. To manage collection size
A
  1. To enable collections to be traversed sequentially

(Förklaring: IEnumerable gör att en samling kan itereras sekventiellt och tillhandahåller en GetEnumerator-metod som returnerar en IEnumerator).

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

What method does IEnumerable<T> provide to obtain an enumerator?</T>

  1. GetItems()
  2. GetEnumerator()
  3. Next()
  4. GetEnumerable()
A
  1. GetEnumerator()

(Förklaring: GetEnumerator() returnerar en enumerator som kan användas för att iterera genom samlingen).

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

In the Iterator pattern, what does the method “MoveNext()” return when there are no more elements?

  1. A null reference
  2. An exception
  3. false
  4. true
A
  1. false

(Förklaring: MoveNext() returnerar false när iteratorn når slutet av samlingen, vilket stoppar iterationen).

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

How do foreach loops work with IEnumerable in C#?

  1. They initialize an IEnumerator and manually call MoveNext()
  2. They iterate over a collection without exposing its underlying structure
  3. They only work with List<T> collections</T>
  4. They require manual index handling
A
  1. They iterate over a collection without exposing its underlying structure

(Förklaring: foreach-loopar använder IEnumerable för att iterera genom en samling utan att exponera dess interna struktur, vilket gör koden enkel och säker).

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

What does the “yield” keyword in C# allow you to do in an iterator?

  1. Pause the iterator function and return a value
  2. Terminate the iterator function immediately
  3. Restart the iterator function from the beginning
  4. Access elements in reverse order
A
  1. Pause the iterator function and return a value

(Förklaring: “yield return” gör att iteratorn pausar och returnerar ett värde, för att fortsätta när nästa element efterfrågas).

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

What happens when the “yield return” statement is encountered in an iterator?

  1. The iterator resets to the beginning
  2. The iterator returns the current value and pauses execution
  3. The iterator throws an exception
  4. The iterator exits without returning a value
A
  1. The iterator returns the current value and pauses execution

(Förklaring: När yield return påträffas, returneras det aktuella värdet och executionen pausar tills nästa element begärs).

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

Which statement can be used to stop an iterator’s execution early?

  1. return false
  2. yield exit
  3. yield break
  4. break
A
  1. yield break

(Förklaring: “yield break” stoppar iteratorn omedelbart och avslutar sekvensen, vilket gör att inga fler element returneras).

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

What is lazy evaluation in the context of iterators?

  1. Elements are computed only when accessed
  2. Elements are precomputed and stored in memory
  3. Iteration is completed in reverse order
  4. All elements are computed upon iterator creation
A
  1. Elements are computed only when accessed

(Förklaring: Lazy evaluation innebär att elementen bara beräknas när de faktiskt efterfrågas, vilket sparar minne och processorkraft).

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

How does lazy evaluation benefit the use of iterators in C#?

  1. It makes iterators faster by precomputing values
  2. It reduces memory usage by computing values only when needed
  3. It enforces strict ordering of operations
  4. It ensures all elements are computed immediately
A
  1. It reduces memory usage by computing values only when needed

(Förklaring: Lazy evaluation minskar minnesanvändningen genom att beräkna värden endast när de behövs, istället för att hålla alla värden i minnet samtidigt).

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

What is an infinite iterator?

  1. An iterator that stops at the last element and restarts
  2. An iterator that repeatedly cycles through elements
  3. An iterator that continually produces elements without an end
  4. An iterator that allows random access to elements
A
  1. An iterator that continually produces elements without an end

(Förklaring: En oändlig iterator genererar element kontinuerligt utan ett förutbestämt slut).

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

Which of the following is a practical use case for an infinite iterator?

  1. Iterating over a fixed-size array
  2. Generating a sequence of Fibonacci numbers indefinitely
  3. Enumerating all elements in a List<T></T>
  4. Sorting elements in an array
A
  1. Generating a sequence of Fibonacci numbers indefinitely

(Förklaring: En oändlig iterator kan användas för att generera en obegränsad sekvens, som en Fibonacci-serie, utan att behöva veta antalet element i förväg).

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

What would this code output?

IEnumerable<int> GetNumbers()
{
yield return 1;
yield return 2;
yield break;
yield return 3;
}</int>

foreach (var num in GetNumbers())
Console.Write(num);

  1. 123
  2. 12
  3. 1
  4. No output
A
  1. 12

(Förklaring: “yield break” avslutar iteratorn, så endast 1 och 2 returneras eftersom 3 aldrig når exekvering).

16
Q

If you call GetEnumerator() on a List<int> collection with elements {10, 20, 30}, and then call MoveNext() twice, what is the value of Current?</int>

  1. 10
  2. 20
  3. 30
  4. null
A
  1. 20

(Förklaring: When you call GetEnumerator() on a List<int> with elements {10, 20, 30}, it returns an enumerator positioned before the first element. Each call to MoveNext() advances the enumerator to the next element making that element accessible through the Current property.</int>

Here’s what happens step-by-step:

Initial Position: After calling GetEnumerator(), the enumerator is positioned before the first element. Current is not defined at this point.
First MoveNext() Call: The enumerator advances to the first element, 10. Now, Current is 10.
Second MoveNext() Call: The enumerator advances to the second element, 20. Now, Current is 20).

17
Q

What does LINQ stand for in C#?

  1. Language Integrated Query
  2. Logic Integrated Query
  3. Library Interactive Query
  4. Linked Interactive Query
A
  1. Language Integrated Query
18
Q

What is the main purpose of LINQ in C#?

  1. To provide a database connection
  2. To offer a query language integrated with C# for manipulating data
  3. To manage files in C#
  4. To compile programs
A
  1. To offer a query language integrated with C# for manipulating data
19
Q

Which of the following is an advantage of using LINQ?

  1. It requires complex syntax
  2. It is only compatible with SQL databases
  3. It provides type safety and integrates directly with C#
  4. It lacks flexibility for different data sources
A
  1. It provides type safety and integrates directly with C#
20
Q

LINQ can be used to query data from which of the following sources?

  1. XML documents only
  2. Relational databases only
  3. Lists, databases, and XML documents
  4. Files only
A
  1. Lists, databases, and XML documents
21
Q

How do lambda expressions commonly function in LINQ?

  1. They initialize collections
  2. They serve as delegates for specifying conditions or transformations
  3. They replace foreach loops
  4. They are used only for method chaining
A
  1. They serve as delegates for specifying conditions or transformations
22
Q

What will this LINQ query output?

List<int> numbers = new List<int> { 2, 4, 6, 8, 10 };
var result = numbers.Where(n => n > 5);
Console.WriteLine(String.Join(", ", result));</int></int>

  1. 2, 4, 6, 8, 10
  2. 6, 8, 10
  3. 4, 6, 8
  4. 5, 10
A
  1. 6, 8, 10