Bird
0
0

Identify the error in this C# code snippet:

medium📝 Debug Q6 of 15
C Sharp (C#) - LINQ Fundamentals
Identify the error in this C# code snippet:
var numbers = new List {1, 2, 3, 4};
var groups = numbers.GroupBy(n => n % 2 == 0);
Console.WriteLine(groups.Count());
AMissing using System.Linq directive
BNo error; code runs and prints 2
CGroupBy requires a key selector returning string, not bool
DCannot call Count() on GroupBy result
Step-by-Step Solution
Solution:
  1. Step 1: Check GroupBy key selector

    The key selector returns a bool, which is valid as a key type.
  2. Step 2: Verify Count() usage

    The result of GroupBy implements IEnumerable, so Count() is valid with System.Linq.
  3. Final Answer:

    No error; code runs and prints 2 -> Option B
  4. Quick Check:

    GroupBy with bool key works and Count() is valid [OK]
Quick Trick: GroupBy key can be any type, including bool [OK]
Common Mistakes:
MISTAKES
  • Assuming key must be string
  • Forgetting to add using System.Linq
  • Thinking Count() is invalid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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