Bird
0
0

What will be the output of this C# code?

medium📝 Predict Output Q5 of 15
C Sharp (C#) - LINQ Fundamentals
What will be the output of this C# code?
var words = new[] {"apple", "banana", "apricot", "blueberry"};
var grouped = words.GroupBy(w => w[0]);
foreach (var group in grouped)
{
    Console.WriteLine($"{group.Key}: {string.Join(",", group)}");
}
Ab: banana,blueberry a: apple,apricot
Ba: apple,apricot b: banana,blueberry
Capple: apple banana: banana apricot: apricot blueberry: blueberry
Da: banana,blueberry b: apple,apricot
Step-by-Step Solution
Solution:
  1. Step 1: Group by first character

    Words are grouped by their first letter: 'a' and 'b'.
  2. Step 2: Determine group contents and order

    Group 'a' has "apple" and "apricot"; group 'b' has "banana" and "blueberry". The order is by first occurrence: 'a' then 'b'.
  3. Final Answer:

    a: apple,apricot b: banana,blueberry -> Option B
  4. Quick Check:

    GroupBy first char groups words correctly [OK]
Quick Trick: GroupBy preserves order of first key seen in source [OK]
Common Mistakes:
MISTAKES
  • Swapping group keys
  • Printing full words as keys
  • Mixing group contents

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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