Bird
0
0

Given the code below, what will be the output?

medium📝 Predict Output Q5 of 15
C Sharp (C#) - LINQ Fundamentals
Given the code below, what will be the output?
var words = new[] {"apple", "banana", "cherry"};
var lengths = from w in words select w.Length;
Console.WriteLine(string.Join(",", lengths));
A6,6,6
Bapple,banana,cherry
C5,6,6
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Understand the select clause

    The query projects the length of each word: "apple"=5, "banana"=6, "cherry"=6.
  2. Step 2: Output formatting

    string.Join joins the lengths with commas, producing "5,6,6".
  3. Final Answer:

    5,6,6 -> Option C
  4. Quick Check:

    Select projects lengths = 5,6,6 [OK]
Quick Trick: Select can project properties like Length [OK]
Common Mistakes:
MISTAKES
  • Printing original words instead of lengths
  • Assuming all lengths are same
  • Expecting compilation error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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