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

List generic collection 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!
query_result
intermediate
2:00remaining
What is the output of this C# List operation?
Consider the following C# code that uses a List of integers. What will be printed to the console?
C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        List<int> numbers = new List<int>() {1, 2, 3, 4, 5};
        numbers.RemoveAt(2);
        Console.WriteLine(numbers.Count);
    }
}
A4
B5
C3
DThrows an exception
Attempts:
2 left
💡 Hint
Removing an item reduces the count by one.
📝 Syntax
intermediate
1:30remaining
Which option correctly adds an element to a List in C#?
You have a List called fruits. Which line of code correctly adds "Apple" to the list?
Afruits.Insert("Apple");
Bfruits.Append("Apple");
Cfruits.Add("Apple");
Dfruits.Push("Apple");
Attempts:
2 left
💡 Hint
The method to add an item at the end of a List is Add.
optimization
advanced
2:30remaining
Which option is the most efficient way to check if a List contains the number 10?
Given a List numbers, which code snippet is the best choice to check if 10 is in the list?
Aif (numbers.Find(x => x == 10) != 0) { /* found */ }
Bif (numbers.IndexOf(10) != -1) { /* found */ }
Cforeach (int n in numbers) { if (n == 10) { /* found */ break; } }
Dif (numbers.Contains(10)) { /* found */ }
Attempts:
2 left
💡 Hint
Look for the method designed to check existence directly.
🔧 Debug
advanced
1:30remaining
What error does this code produce?
Examine the following code snippet. What error will it cause when run?
C Sharp (C#)
List<string> names = null;
names.Add("John");
ANullReferenceException
BNo error, adds "John"
CInvalidOperationException
DArgumentNullException
Attempts:
2 left
💡 Hint
Think about what happens when you call a method on a null object.
🧠 Conceptual
expert
2:00remaining
What is the time complexity of accessing an element by index in a List?
In C#, what is the time complexity to access an element at a specific index in a List?
AO(n) - Linear time
BO(1) - Constant time
CO(log n) - Logarithmic time
DO(n^2) - Quadratic time
Attempts:
2 left
💡 Hint
Think about how List stores elements internally.