wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Collections, Searching and Sorting algorithms

Total questions: 15

Worksheet time: 8mins

Name
Class
Date
1.

Which of the following is a generic collection in .NET?

a)

ArrayList

b)

Hashtable

c)

List<T>

d)

Stack

2.

What is the primary benefit of using a generic collection like List<T>

a)

Easier syntax

b)

No need for boxing/unboxing

c)

Slower performance

d)

Fixed size

3.

What is the return value of IndexOf() if the element is not found in a list?

a)

0

b)

null

c)

-1

d)

Exception is thrown

4.

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);

a)

3

b)

4

c)

5

d)

9

5.

What does the following search return?

List<int> nums = new List<int> { 10, 20, 30 };

int index = nums.IndexOf(20);

Console.WriteLine(index);

a)

1

b)

2

c)

20

d)

-1

6.

What does this LINQ query return?

var items = new List<int> { 5, 10, 15 };

var result = items.FirstOrDefault(x => x > 7);

Console.WriteLine(result);

a)

5

b)

10

c)

15

d)

0

7.

Which collection type maintains insertion order and allows duplicates?

var list = new List<int> { 1, 2, 2, 3 };

a)

HashSet

b)

Dictionary

c)

List

d)

Stack

8.

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 + " ");

a)

Three One Two

b)

One Two Three

c)

Error

d)

Two One Three

9.

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);

a)

15

b)

30

c)

20

d)

25

10.

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);

a)

Updates key "A" with new value

b)

Compiles but skips second entry

c)

Runtime exception

d)

Adds both entries

11.

What will the following print?

var arr = new[] { 8, 3, 6, 1 };

Array.Sort(arr, (a, b) => b.CompareTo(a));

Console.WriteLine(arr[2]);

a)

1

b)

3

c)

6

d)

8

12.

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";

a)

GetValue()

b)

ContainsKey()

c)

TryGetValue()

d)

TryGet()

13.

What is the output?

var set = new HashSet<int> { 1, 2, 3 };

set.Add(2);

Console.WriteLine(set.Count);

a)

2

b)

3

c)

4

d)

error

14.

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)

A

b)

B

c)

null

d)

error

15.

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();

a)

Sorts alphabetically by Name

b)

Sorts in reverse orderSorts in reverse order

c)

Runtime exception

d)

Compiles but has no effect