NEW
Font size
WorksheetsCollections, Searching and Sorting algorithms
Total questions: 15
Worksheet time: 8mins
Which of the following is a generic collection in .NET?
ArrayList
Hashtable
List<T>
Stack
What is the primary benefit of using a generic collection like List<T>
Easier syntax
No need for boxing/unboxing
Slower performance
Fixed size
What is the return value of IndexOf() if the element is not found in a list?
0
null
-1
Exception is thrown
What will be the output of the following code?
List<int> numbers = new List<int> { 1, 3, 5, 7 };
numbers.Add(9);
Console.WriteLine(numbers.Count);
3
4
5
9
What does the following search return?
List<int> nums = new List<int> { 10, 20, 30 };
int index = nums.IndexOf(20);
Console.WriteLine(index);
1
2
20
-1
What does this LINQ query return?
var items = new List<int> { 5, 10, 15 };
var result = items.FirstOrDefault(x => x > 7);
Console.WriteLine(result);
5
10
15
0
Which collection type maintains insertion order and allows duplicates?
var list = new List<int> { 1, 2, 2, 3 };
HashSet
Dictionary
List
Stack
What is the output of the following code using SortedDictionary?
SortedDictionary<int, string> dict = new SortedDictionary<int, string>();
dict.Add(3, "Three");
dict.Add(1, "One");
dict.Add(2, "Two");
foreach (var pair in dict)
Console.Write(pair.Value + " ");
Three One Two
One Two Three
Error
Two One Three
What will be the result of this code involving LinkedList?
LinkedList<int> list = new LinkedList<int>();
list.AddLast(10); list.AddLast(20);
list.AddFirst(5);
Console.WriteLine(list.First.Value + list.Last.Value);
15
30
20
25
What happens when you try to add a duplicate key in Dictionary?
Dictionary<string, int> data = new Dictionary<string, int>();
data.Add("A", 1);
data.Add("A", 2);
Updates key "A" with new value
Compiles but skips second entry
Runtime exception
Adds both entries
What will the following print?
var arr = new[] { 8, 3, 6, 1 };
Array.Sort(arr, (a, b) => b.CompareTo(a));
Console.WriteLine(arr[2]);
1
3
6
8
Which method would you use to avoid exception if key might not exist in Dictionary?
Dictionary<string, string> capitals = new Dictionary<string, string>();
capitals["France"] = "Paris";
capitals["India"] = "Delhi";
GetValue()
ContainsKey()
TryGetValue()
TryGet()
What is the output?
var set = new HashSet<int> { 1, 2, 3 };
set.Add(2);
Console.WriteLine(set.Count);
2
3
4
error
What will be printed by this queue-based code?
Queue<string> q = new Queue<string>();
q.Enqueue("A");
q.Enqueue("B");
q.Dequeue();
Console.WriteLine(q.Peek());
A
B
null
error
What happens when you sort a list of objects without IComparable implementation?
class Person { public string Name; }
List<Person> people = new List<Person>
{
new Person { Name = "Ram" },
new Person { Name = "Sam" }
};
people.Sort();
Sorts alphabetically by Name
Sorts in reverse orderSorts in reverse order
Runtime exception
Compiles but has no effect
