0
0
C Sharp (C#)programming~20 mins

List methods (Add, Remove, Find, Sort) in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
List Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this C# code using List.Add and List.Remove?

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?

C Sharp (C#)
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 + " ");
        }
    }
}
AApple Banana Cherry
BBanana Cherry
CApple Cherry
DApple Cherry Banana
Attempts:
2 left
💡 Hint

Remember that Remove deletes the first matching item from the list.

Predict Output
intermediate
2:00remaining
What is the output of this C# code using List.Find?

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?

C Sharp (C#)
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);
    }
}
A10
B20
C40
D30
Attempts:
2 left
💡 Hint

Find returns the first element that matches the condition.

Predict Output
advanced
2:00remaining
What is the output after sorting this list of strings?

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?

C Sharp (C#)
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 + " ");
        }
    }
}
AMike Anna Zoe
BAnna Mike Zoe
CZoe Anna Mike
DMike Zoe Anna
Attempts:
2 left
💡 Hint

Sort arranges strings in alphabetical order.

Predict Output
advanced
2:00remaining
What error does this code raise when removing an item not in the list?

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?

C Sharp (C#)
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);
    }
}
AFalse
BArgumentOutOfRangeException
CTrue
DNullReferenceException
Attempts:
2 left
💡 Hint

Remove returns a boolean indicating success or failure.

🧠 Conceptual
expert
3:00remaining
How many items remain after this sequence of List operations?

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?

C Sharp (C#)
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);
    }
}
A3
B4
C5
D2
Attempts:
2 left
💡 Hint

Track each operation carefully: removal, addition, sorting, then removal again.