Bird
0
0

What will be the output of this code snippet?

medium📝 Predict Output Q5 of 15
C Sharp (C#) - LINQ Fundamentals
What will be the output of this code snippet?
var words = new List<string> { "apple", "banana", "pear" };
var sorted = words.OrderByDescending(w => w.Length);
foreach(var w in sorted) Console.Write(w + " ");
Apear banana apple
Bbanana apple pear
Capple pear banana
Dbanana pear apple
Step-by-Step Solution
Solution:
  1. Step 1: Analyze sorting key and order

    Sorting by string length descending orders words by length: banana(6), apple(5), pear(4).
  2. Step 2: Determine output sequence

    The foreach prints: "banana apple pear "
  3. Final Answer:

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

    OrderByDescending by length sorts longest first [OK]
Quick Trick: OrderByDescending sorts largest key first [OK]
Common Mistakes:
MISTAKES
  • Sorting ascending instead of descending
  • Sorting by word alphabet instead of length
  • Confusing output order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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