Consider the following C# code snippet:
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> fruits = new List<string>();
fruits.Add("Apple");
fruits.Add("Banana");
fruits.Add("Cherry");
fruits.Remove("Banana");
foreach (var fruit in fruits) {
Console.Write(fruit + " ");
}
}
}What will be printed when this code runs?
using System; using System.Collections.Generic; class Program { static void Main() { List<string> fruits = new List<string>(); fruits.Add("Apple"); fruits.Add("Banana"); fruits.Add("Cherry"); fruits.Remove("Banana"); foreach (var fruit in fruits) { Console.Write(fruit + " "); } } }
Remember that Remove deletes the first matching item from the list.
The code adds three fruits to the list: Apple, Banana, and Cherry. Then it removes "Banana". So the list contains "Apple" and "Cherry". The foreach loop prints these with spaces.
Look at this C# code:
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int> { 10, 20, 30, 40, 50 };
int found = numbers.Find(x => x > 25);
Console.WriteLine(found);
}
}What number will be printed?
using System; using System.Collections.Generic; class Program { static void Main() { List<int> numbers = new List<int> { 10, 20, 30, 40, 50 }; int found = numbers.Find(x => x > 25); Console.WriteLine(found); } }
Find returns the first element that matches the condition.
The list has numbers 10, 20, 30, 40, 50. The condition is > 25. The first number greater than 25 is 30, so 30 is printed.
Examine this C# code:
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> names = new List<string> { "Zoe", "Anna", "Mike" };
names.Sort();
foreach (var name in names) {
Console.Write(name + " ");
}
}
}What will be printed?
using System; using System.Collections.Generic; class Program { static void Main() { List<string> names = new List<string> { "Zoe", "Anna", "Mike" }; names.Sort(); foreach (var name in names) { Console.Write(name + " "); } } }
Sort arranges strings in alphabetical order.
The list is sorted alphabetically: Anna, Mike, Zoe. The foreach prints them in this order.
Consider this C# code:
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> nums = new List<int> { 1, 2, 3 };
bool removed = nums.Remove(5);
Console.WriteLine(removed);
}
}What will be printed or what error occurs?
using System; using System.Collections.Generic; class Program { static void Main() { List<int> nums = new List<int> { 1, 2, 3 }; bool removed = nums.Remove(5); Console.WriteLine(removed); } }
Remove returns a boolean indicating success or failure.
Since 5 is not in the list, Remove returns false. No exception is thrown.
Given this C# code:
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> data = new List<int> { 5, 10, 15, 20, 25 };
data.RemoveAll(x => x % 10 == 0);
data.Add(30);
data.Sort();
data.Remove(15);
Console.WriteLine(data.Count);
}
}How many items will be printed?
using System; using System.Collections.Generic; class Program { static void Main() { List<int> data = new List<int> { 5, 10, 15, 20, 25 }; data.RemoveAll(x => x % 10 == 0); data.Add(30); data.Sort(); data.Remove(15); Console.WriteLine(data.Count); } }
Track each operation carefully: removal, addition, sorting, then removal again.
Initial list: 5,10,15,20,25
RemoveAll removes numbers divisible by 10 (10 and 20) → 5,15,25
Add 30 → 5,15,25,30
Sort → 5,15,25,30
Remove 15 → 5,25,30
Count is 3.