Bird
0
0

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

medium📝 Predict Output Q4 of 15
C Sharp (C#) - Collections
What will be the output of the following C# code?
var animals = new List<string> {"dog", "cat", "bird"};
animals.Remove("cat");
animals.Sort();
foreach(var animal in animals) Console.Write(animal + " ");
Adog bird
Bbird dog
Ccat dog bird
Ddog cat bird
Step-by-Step Solution
Solution:
  1. Step 1: Remove "cat"

    After animals.Remove("cat"), list is ["dog", "bird"].
  2. Step 2: Sort the list

    Sorting alphabetically results in ["bird", "dog"].
  3. Step 3: Output elements

    Printing each with a space gives: "bird dog "
  4. Final Answer:

    bird dog -> Option B
  5. Quick Check:

    Remove then sort alphabetically [OK]
Quick Trick: Remove item then Sort() orders list alphabetically [OK]
Common Mistakes:
MISTAKES
  • Forgetting to remove the item
  • Assuming Sort() sorts descending
  • Confusing order of elements after sort

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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