Bird
Raised Fist0

What will be the output of the following code?

medium📝 Predict Output Q4 of Q15
C Sharp (C#) - LINQ Fundamentals
What will be the output of the following code?
int[] numbers = {1, 2, 3, 4, 5};
var query = from n in numbers where n % 2 == 0 select n;
foreach(var num in query) Console.Write(num + " ");
A1 3 5
B1 2 3 4 5
C2 4
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the LINQ query filtering condition

    The query filters numbers where n % 2 == 0, meaning even numbers only.
  2. Step 2: Identify even numbers in the array

    From {1,2,3,4,5}, the even numbers are 2 and 4.
  3. Final Answer:

    The output will be '2 4 ' as only even numbers are selected. -> Option C
  4. Quick Check:

    LINQ where even numbers = 2 4 [OK]
Quick Trick: Use modulo (%) to filter even numbers in LINQ [OK]
Common Mistakes:
MISTAKES
  • Selecting odd numbers by mistake
  • Printing all numbers ignoring filter
  • No output due to syntax error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes