Bird
Raised Fist0

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

medium📝 Predict Output Q13 of Q15
C Sharp (C#) - Collections
What will be the output of the following C# code?
var numbers = new List<int> {5, 3, 8, 1};
numbers.Sort();
Console.WriteLine(string.Join(",", numbers));
A5,3,8,1
B1,3,5,8
C8,5,3,1
D3,5,1,8
Step-by-Step Solution
Solution:
  1. Step 1: Understand what Sort does

    Sort arranges the list items in ascending order.
  2. Step 2: Apply Sort to the list

    The list {5, 3, 8, 1} sorted ascending becomes {1, 3, 5, 8}.
  3. Final Answer:

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

    Sort orders numbers ascending [OK]
Quick Trick: Sort arranges numbers from smallest to largest [OK]
Common Mistakes:
MISTAKES
  • Assuming Sort reverses the list
  • Confusing Sort with Find
  • Expecting original order after Sort

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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