Bird
Raised Fist0

Which of the following is the correct way to group a list of integers by their value modulo 5 using LINQ in C#?

easy📝 Syntax Q3 of Q15
C Sharp (C#) - LINQ Fundamentals
Which of the following is the correct way to group a list of integers by their value modulo 5 using LINQ in C#?
Avar groups = numbers.GroupBy(n => n + 5);
Bvar groups = numbers.GroupBy(n => n / 5);
Cvar groups = numbers.GroupBy(n => n * 5);
Dvar groups = numbers.GroupBy(n => n % 5);
Step-by-Step Solution
Solution:
  1. Step 1: Understand grouping key

    We want to group numbers by their remainder when divided by 5, which is done by n % 5.
  2. Step 2: Check syntax

    The correct syntax is GroupBy(n => n % 5). Other options use division, multiplication, or addition which do not group by remainder.
  3. Final Answer:

    var groups = numbers.GroupBy(n => n % 5); -> Option D
  4. Quick Check:

    Grouping by modulo 5 means using n % 5 [OK]
Quick Trick: GroupBy key must be the grouping criterion [OK]
Common Mistakes:
MISTAKES
  • Using division instead of modulo for grouping
  • Using arithmetic operations that don't produce grouping keys
  • Confusing GroupBy syntax with Where or Select

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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