Bird
0
0

What will be the output of the following C# code?

medium📝 Predict Output Q4 of 15
C Sharp (C#) - LINQ Fundamentals
What will be the output of the following C# code?
var fruits = new[] {"apple", "banana", "apricot", "blueberry"};
var grouped = fruits.GroupBy(f => f.Length);
foreach (var group in grouped)
{
    Console.WriteLine($"Length {group.Key}: {string.Join(",", group)}");
}
ALength 5: apple Length 6: banana,apricot Length 9: blueberry
BLength 5: apple,apricot Length 6: banana Length 9: blueberry
CLength 5: apple Length 6: banana Length 7: apricot Length 9: blueberry
DLength 5: apple Length 6: banana Length 7: apricot,blueberry
Step-by-Step Solution
Solution:
  1. Step 1: Determine lengths of each fruit

    "apple" (5), "banana" (6), "apricot" (7), "blueberry" (9).
  2. Step 2: Check grouping key

    Grouping is by f.Length, so groups are by length.
  3. Step 3: Verify group contents

    Group 5: "apple" only
    Group 6: "banana" only
    Group 7: "apricot" only
    Group 9: "blueberry" only
  4. Step 4: Match output

    Length 5: apple Length 6: banana,apricot Length 9: blueberry incorrectly groups "banana" and "apricot" together under length 6, but "apricot" length is 7, so Length 5: apple Length 6: banana,apricot Length 9: blueberry is incorrect.
    Length 5: apple,apricot Length 6: banana Length 9: blueberry groups "apple" and "apricot" under length 5, which is wrong.
    Length 5: apple Length 6: banana Length 7: apricot Length 9: blueberry correctly lists lengths and groups:
    Length 5: apple
    Length 6: banana
    Length 7: apricot
    Length 9: blueberry
  5. Final Answer:

    Option C -> Option C
  6. Quick Check:

    Check string lengths carefully [OK]
Quick Trick: Count string lengths correctly for grouping [OK]
Common Mistakes:
MISTAKES
  • Miscounting string lengths
  • Assuming multiple items share same length incorrectly
  • Confusing group keys with group contents

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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