Bird
0
0

What will be the output of this code snippet?

medium📝 Predict Output Q4 of 15
C Sharp (C#) - LINQ Fundamentals
What will be the output of this code snippet?
int[] nums = {1, 2, 3, 4};
var evens = from n in nums where n % 2 == 0 select n;
Console.WriteLine(string.Join(",", evens));
ANo output
B1,3
C1,2,3,4
D2,4
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the LINQ query

    The query selects numbers from nums where the number is even (n % 2 == 0).
  2. Step 2: Identify even numbers in array

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

    2,4 -> Option D
  4. Quick Check:

    LINQ filter even numbers = 2,4 [OK]
Quick Trick: Use 'where' to filter items in LINQ [OK]
Common Mistakes:
MISTAKES
  • Selecting odd numbers by mistake
  • Printing original array instead of filtered
  • Forgetting to use 'where' clause

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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