Challenge - 5 Problems
Deferred Execution Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of deferred execution with LINQ query
What is the output of this C# code snippet that uses deferred execution in LINQ?
C Sharp (C#)
List<int> numbers = new() {1, 2, 3}; var query = numbers.Select(x => x * 2); numbers.Add(4); foreach (var num in query) { Console.Write(num + " "); }
Attempts:
2 left
💡 Hint
Remember that LINQ queries are not executed until you enumerate them.
✗ Incorrect
The LINQ query uses deferred execution, so when enumerated, it reflects the current state of the source list, which includes the newly added element 4. Thus, the output doubles all four elements.
❓ Predict Output
intermediate2:00remaining
Effect of modifying source collection after query creation
What will be printed by this C# program?
C Sharp (C#)
var list = new List<string> {"a", "b", "c"}; var filtered = list.Where(s => s != "b"); list.Remove("a"); foreach (var item in filtered) { Console.Write(item); }
Attempts:
2 left
💡 Hint
Consider when the Where filter is applied and the current state of the list.
✗ Incorrect
The Where query is deferred and uses the current list state when enumerated. After removing "a", the list contains "b" and "c". The filter excludes "b", so only "c" is printed.
🔧 Debug
advanced2:00remaining
Why does this deferred execution code print unexpected results?
Consider this code snippet:
List nums = new() {1, 2, 3};
var query = nums.Select(x => x + 1);
nums = new List {4, 5, 6};
foreach (var n in query)
{
Console.Write(n + " ");
}
Why does the output not reflect the new list values?
C Sharp (C#)
List<int> nums = new() {1, 2, 3}; var query = nums.Select(x => x + 1); nums = new List<int> {4, 5, 6}; foreach (var n in query) { Console.Write(n + " "); }
Attempts:
2 left
💡 Hint
Think about what the query captures and what the variable reassignment does.
✗ Incorrect
The query captures the original list object. Reassigning the variable nums to a new list does not affect the original list referenced by the query. So the query enumerates the original list {1,2,3}, producing 2 3 4.
📝 Syntax
advanced2:00remaining
Which LINQ query causes a runtime error due to deferred execution?
Given the following code, which option will cause an exception when enumerated?
C Sharp (C#)
List<int> numbers = new() {1, 2, 3}; var query = numbers.Select(x => 10 / x); foreach (var val in query) { Console.Write(val + " "); }
Attempts:
2 left
💡 Hint
Check for division by zero possibilities during enumeration.
✗ Incorrect
Option A causes division by zero when x is 1, because 1 - 1 = 0, causing a runtime DivideByZeroException during deferred execution.
🚀 Application
expert3:00remaining
Predict the final count after deferred execution with side effects
What is the value of count printed by this C# program?
C Sharp (C#)
List<int> data = new() {1, 2, 3, 4}; int count = 0; var query = data.Where(x => { count++; return x % 2 == 0; }); // No enumeration here count += 10; foreach (var item in query) { // Just iterate } Console.Write(count);
Attempts:
2 left
💡 Hint
Remember when the lambda inside Where is executed and how many elements it processes.
✗ Incorrect
The lambda runs once per element during enumeration. The count is incremented 10 before enumeration, then 4 times during enumeration (for each element). So final count is 10 + 4 = 14.