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

Immediate execution methods in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Immediate Execution Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Count() on a filtered list
What is the output of this C# code using LINQ's immediate execution method Count()?
C Sharp (C#)
using System;
using System.Linq;

class Program {
    static void Main() {
        int[] numbers = {1, 2, 3, 4, 5};
        int count = numbers.Where(n => n > 3).Count();
        Console.WriteLine(count);
    }
}
A0
B2
C3
D5
Attempts:
2 left
💡 Hint
Count() returns how many items match the condition immediately.
❓ Predict Output
intermediate
2:00remaining
Result of ToList() on a query
What will be printed by this C# program using ToList() for immediate execution?
C Sharp (C#)
using System;
using System.Linq;
using System.Collections.Generic;

class Program {
    static void Main() {
        var words = new List<string> {"apple", "banana", "cherry"};
        var filtered = words.Where(w => w.Contains('a')).ToList();
        Console.WriteLine(filtered.Count);
    }
}
A2
B3
C1
D0
Attempts:
2 left
💡 Hint
ToList() creates a list immediately with all matching items.
❓ Predict Output
advanced
2:00remaining
Output of First() with condition
What does this C# code print when using First() as an immediate execution method?
C Sharp (C#)
using System;
using System.Linq;

class Program {
    static void Main() {
        int[] nums = {10, 20, 30, 40};
        int first = nums.Where(n => n > 15).First();
        Console.WriteLine(first);
    }
}
A20
B10
C30
DThrows InvalidOperationException
Attempts:
2 left
💡 Hint
First() returns the first element that matches the condition immediately.
❓ Predict Output
advanced
2:00remaining
Output of Single() with one matching element
What will this C# program print using Single() as an immediate execution method?
C Sharp (C#)
using System;
using System.Linq;

class Program {
    static void Main() {
        int[] values = {5, 10, 15};
        int single = values.Single(v => v == 10);
        Console.WriteLine(single);
    }
}
A15
B5
CThrows InvalidOperationException
D10
Attempts:
2 left
💡 Hint
Single() returns the only element matching the condition or throws if none or multiple.
❓ Predict Output
expert
2:00remaining
Output of Count() after modifying source collection
What is the output of this C# program that uses Count() immediate execution after modifying the source array?
C Sharp (C#)
using System;
using System.Linq;

class Program {
    static void Main() {
        int[] data = {1, 2, 3, 4};
        var query = data.Where(x => x > 2);
        data[2] = 10; // change 3 to 10
        int count = query.Count();
        Console.WriteLine(count);
    }
}
A3
B1
C2
D0
Attempts:
2 left
💡 Hint
Count() evaluates the query immediately using the current state of the data.

Practice

(1/5)
1. Which of the following methods in C# causes immediate execution of a LINQ query?
easy
A. ToList()
B. Where()
C. Select()
D. OrderBy()

Solution

  1. Step 1: Understand deferred vs immediate execution

    Methods like Where(), Select(), and OrderBy() only build the query and do not execute it immediately.
  2. Step 2: Identify immediate execution methods

    ToList() forces the query to run and returns a list immediately.
  3. Final Answer:

    ToList() -> Option A
  4. Quick Check:

    Immediate execution = ToList() [OK]
Hint: Immediate methods like ToList() run queries right away [OK]
Common Mistakes:
  • Confusing deferred methods like Where() as immediate
  • Thinking Select() executes query immediately
  • Assuming OrderBy() runs query instantly
2. Which of the following is the correct syntax to immediately get the count of elements in a LINQ query numbers?
easy
A. int count = numbers.Count;
B. int count = Count(numbers);
C. int count = numbers.Count();
D. int count = numbers.Count[];

Solution

  1. Step 1: Recognize Count() as a method

    In LINQ, Count() is a method that must be called with parentheses to execute immediately.
  2. Step 2: Check syntax correctness

    numbers.Count() is the correct syntax; Count without parentheses is a property, which does not exist here.
  3. Final Answer:

    int count = numbers.Count(); -> Option C
  4. Quick Check:

    Count() method needs parentheses [OK]
Hint: Count() always needs parentheses to execute immediately [OK]
Common Mistakes:
  • Using Count without parentheses
  • Trying to call Count as a standalone function
  • Using invalid syntax like Count[]
3. What is the output of the following code?
var numbers = new List<int> {1, 2, 3, 4};
var result = numbers.Where(n => n > 2).Count();
Console.WriteLine(result);
medium
A. 0
B. 3
C. 4
D. 2

Solution

  1. Step 1: Filter numbers greater than 2

    The Where method filters numbers: 3 and 4 satisfy the condition, so filtered list is [3,4].
  2. Step 2: Count the filtered elements

    Count() returns the number of elements in the filtered list, which is 2.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    Count of numbers > 2 = 2 [OK]
Hint: Count() after Where() counts filtered items immediately [OK]
Common Mistakes:
  • Counting original list length instead of filtered
  • Confusing Count() with deferred execution
  • Assuming Where() returns count directly
4. Identify the error in this code snippet:
var list = new List<string> {"apple", "banana"};
var first = list.First;
Console.WriteLine(first);
medium
A. Missing parentheses after First method
B. List initialization syntax is wrong
C. Console.WriteLine cannot print strings
D. First method does not exist on List

Solution

  1. Step 1: Check usage of First method

    First is a method and requires parentheses to execute immediately.
  2. Step 2: Identify syntax error

    Code uses list.First without parentheses, which causes a compile-time error.
  3. Final Answer:

    Missing parentheses after First method -> Option A
  4. Quick Check:

    First() needs () to run immediately [OK]
Hint: Always add () when calling immediate execution methods [OK]
Common Mistakes:
  • Forgetting parentheses on First()
  • Thinking First is a property
  • Misunderstanding List initialization syntax
5. Given a list of integers, how can you immediately get a list of only even numbers using LINQ and immediate execution methods?
hard
A. var evens = numbers.Where(n => n % 2 == 0);
B. var evens = numbers.Where(n => n % 2 == 0).ToList();
C. var evens = numbers.Select(n => n % 2 == 0).ToList();
D. var evens = numbers.ToList().Where(n => n % 2 == 0);

Solution

  1. Step 1: Filter even numbers with Where()

    Where(n => n % 2 == 0) filters the list to only even numbers.
  2. Step 2: Use ToList() for immediate execution

    Calling ToList() after filtering runs the query immediately and returns a list of even numbers.
  3. Final Answer:

    var evens = numbers.Where(n => n % 2 == 0).ToList(); -> Option B
  4. Quick Check:

    Filter then ToList() = immediate even list [OK]
Hint: Chain Where() then ToList() for immediate filtered list [OK]
Common Mistakes:
  • Not calling ToList() to force immediate execution
  • Calling ToList() before Where(), which returns full list
  • Using Select() incorrectly to filter
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