Challenge - 5 Problems
List Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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); } }
Attempts:
2 left
💡 Hint
Removing an item reduces the count by one.
✗ Incorrect
The list starts with 5 elements. Removing the element at index 2 removes one item, so the count becomes 4.
📝 Syntax
intermediate1: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?
Attempts:
2 left
💡 Hint
The method to add an item at the end of a List is Add.
✗ Incorrect
The Add method adds an element to the end of the List. Append is not a List method, Insert requires an index, and Push is not a List method.
❓ optimization
advanced2: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?
Attempts:
2 left
💡 Hint
Look for the method designed to check existence directly.
✗ Incorrect
Contains is optimized to check if an item exists and returns a boolean. IndexOf returns an index which is less direct. The foreach loop is less concise and slower. Find returns the element or default, which can be confusing if 0 is a valid element.
🔧 Debug
advanced1: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");
Attempts:
2 left
💡 Hint
Think about what happens when you call a method on a null object.
✗ Incorrect
The list 'names' is null, so calling Add on it causes a NullReferenceException.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about how List stores elements internally.
✗ Incorrect
List uses an array internally, so accessing by index is constant time O(1).