Bird
Raised Fist0

What will be the output of the following code?

medium📝 Predict Output Q4 of Q15
C Sharp (C#) - LINQ Fundamentals
What will be the output of the following code?
var numbers = new List<int> { 5, 3, 8, 1 };
var sorted = numbers.OrderBy(n => n);
foreach(var num in sorted) Console.Write(num + " ");
A1 3 5 8
B5 3 8 1
C8 5 3 1
D1 5 3 8
Step-by-Step Solution
Solution:
  1. Step 1: Understand OrderBy behavior

    The OrderBy method sorts the list in ascending order: 1, 3, 5, 8.
  2. Step 2: Confirm output format

    The foreach prints each number followed by a space, so output is "1 3 5 8 "
  3. Final Answer:

    1 3 5 8 -> Option A
  4. Quick Check:

    OrderBy sorts ascending, output matches [OK]
Quick Trick: OrderBy sorts ascending; output prints sorted numbers [OK]
Common Mistakes:
MISTAKES
  • Expecting original order to print
  • Confusing OrderBy with OrderByDescending
  • Forgetting that OrderBy returns IEnumerable, not List

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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