Bird
Raised Fist0
C Sharp (C#)programming~20 mins

Deferred execution behavior in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Deferred Execution Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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 + " ");
}
A2 4 6 8
B2 4 6
C1 2 3 4
DRuntime error
Attempts:
2 left
💡 Hint
Remember that LINQ queries are not executed until you enumerate them.
❓ Predict Output
intermediate
2: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);
}
Abc
Bc
Cab
DRuntime exception
Attempts:
2 left
💡 Hint
Consider when the Where filter is applied and the current state of the list.
🔧 Debug
advanced
2: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 + " ");
}
ANo output
B5 6 7
CRuntime error
D2 3 4
Attempts:
2 left
💡 Hint
Think about what the query captures and what the variable reassignment does.
📝 Syntax
advanced
2: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 + " ");
}
Avar query = numbers.Select(x => 10 / (x - 1));
Bvar query = numbers.Select(x => 10 / x);
Cvar query = numbers.Select(x => 10 / (x + 1));
Dvar query = numbers.Select(x => 10 / (x - 2));
Attempts:
2 left
💡 Hint
Check for division by zero possibilities during enumeration.
🚀 Application
expert
3: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);
A10
B12
C14
DRuntime error
Attempts:
2 left
💡 Hint
Remember when the lambda inside Where is executed and how many elements it processes.

Practice

(1/5)
1. What does deferred execution mean in C# LINQ queries?
easy
A. The query runs only when you actually use its results.
B. The query runs immediately when it is defined.
C. The query results are stored permanently in memory.
D. The query runs multiple times automatically without control.

Solution

  1. Step 1: Understand when LINQ queries run

    LINQ queries in C# do not run when defined but wait until their results are needed.
  2. Step 2: Identify deferred execution meaning

    This means the query is executed only when you iterate over it or call methods like Count().
  3. Final Answer:

    The query runs only when you actually use its results. -> Option A
  4. Quick Check:

    Deferred execution = run when used [OK]
Hint: Deferred means delay running until results are needed [OK]
Common Mistakes:
  • Thinking queries run immediately when defined
  • Assuming results are stored permanently
  • Believing queries run automatically multiple times
2. Which of the following C# code snippets correctly triggers deferred execution of a LINQ query?
easy
A. var result = numbers.Where(n => n > 5);
B. var filtered = numbers.Where(n => n < 3);
C. var query = numbers.Select(n => n * 2);
D. var count = numbers.Where(n => n > 5).Count();

Solution

  1. Step 1: Identify code that triggers execution

    Calling Count() forces the query to run immediately and count matching elements.
  2. Step 2: Compare other options

    The other options only define queries but do not execute them until iterated or a method like Count() is called.
  3. Final Answer:

    var count = numbers.Where(n => n > 5).Count(); -> Option D
  4. Quick Check:

    Calling Count() triggers execution [OK]
Hint: Methods like Count() force query to run immediately [OK]
Common Mistakes:
  • Assuming query runs when assigned to a variable
  • Confusing query definition with execution
  • Not knowing which methods trigger execution
3. What will be the output of this C# code?
var numbers = new List<int> {1, 2, 3};
var query = numbers.Select(n => n * 2);
numbers.Add(4);
foreach(var num in query) Console.Write(num + " ");
medium
A. 2 4 6 8
B. 2 4 6
C. 1 2 3 4
D. 4 6 8

Solution

  1. Step 1: Understand deferred execution with Select

    The query uses deferred execution, so it runs when the foreach starts, not when defined.
  2. Step 2: Check list changes before execution

    Since numbers.Add(4) happens before foreach, the query sees the updated list including 4.
  3. Final Answer:

    2 4 6 8 -> Option A
  4. Quick Check:

    Deferred execution includes added item [OK]
Hint: Deferred queries reflect list changes before iteration [OK]
Common Mistakes:
  • Thinking query runs at definition ignoring added items
  • Confusing output with original list values
  • Ignoring that foreach triggers execution
4. Identify the error in this code that prevents deferred execution from working as expected:
var numbers = new List<int> {1, 2, 3};
var query = numbers.Where(n => n > 1).ToList();
numbers.Add(4);
foreach(var num in query) Console.Write(num + " ");
medium
A. foreach loop syntax is invalid.
B. Using ToList() executes query immediately, so added item is ignored.
C. Cannot add items to a list after query definition.
D. The Where clause syntax is incorrect.

Solution

  1. Step 1: Check effect of ToList()

    Calling ToList() executes the query immediately and stores results in a new list.
  2. Step 2: Analyze list modification after execution

    Adding 4 to numbers after ToList() does not affect query because it is already a fixed list.
  3. Final Answer:

    Using ToList() executes query immediately, so added item is ignored. -> Option B
  4. Quick Check:

    ToList() disables deferred execution [OK]
Hint: ToList() runs query immediately, no deferred execution [OK]
Common Mistakes:
  • Thinking ToList() keeps query deferred
  • Believing list changes affect ToList() results
  • Misreading Where clause syntax
5. You want to create a LINQ query that filters out numbers less than 5 and then doubles them, but you also want to avoid re-running the query multiple times for performance. Which approach correctly applies deferred execution but avoids repeated execution?
hard
A. Use a foreach loop inside the query to process items.
B. Define the query with var q = numbers.Where(n => n >= 5).Select(n => n * 2); and use it multiple times directly.
C. Define the query and immediately call ToList() to store results: var q = numbers.Where(n => n >= 5).Select(n => n * 2).ToList();
D. Call Count() on the query before using it to force execution.

Solution

  1. Step 1: Understand deferred execution and repeated use

    Using the query directly runs it every time you iterate, which can be slow if repeated.
  2. Step 2: Use ToList() to cache results

    Calling ToList() runs the query once and stores results, so repeated use is fast without re-executing.
  3. Final Answer:

    Define the query and immediately call ToList() to store results. -> Option C
  4. Quick Check:

    ToList() caches results, avoids repeated execution [OK]
Hint: Use ToList() to run once and reuse results efficiently [OK]
Common Mistakes:
  • Using query directly causing multiple executions
  • Misusing foreach inside query definition
  • Calling Count() only counts, does not cache results
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard