Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q13 of 15
C Sharp (C#) - LINQ Fundamentals
What will be the output of the following code?
var fruits = new List<string> { "banana", "apple", "cherry" };
var sorted = fruits.OrderBy(f => f);
foreach(var fruit in sorted) {
    Console.Write(fruit + " ");
}
Abanana apple cherry
Bapple banana cherry
Ccherry banana apple
Dapple cherry banana
Step-by-Step Solution
Solution:
  1. Step 1: Understand the sorting key

    The code sorts the list of fruits alphabetically by their string value.
  2. Step 2: Determine the sorted order

    Alphabetically, "apple" comes before "banana", which comes before "cherry".
  3. Final Answer:

    apple banana cherry -> Option B
  4. Quick Check:

    OrderBy sorts strings alphabetically [OK]
Quick Trick: OrderBy sorts strings alphabetically ascending [OK]
Common Mistakes:
MISTAKES
  • Assuming original order is preserved
  • Confusing OrderBy with OrderByDescending
  • Not recognizing alphabetical order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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