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

Why collections over arrays in C Sharp (C#) - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Collections Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of modifying a List vs an Array
What will be the output of this C# code snippet?
C Sharp (C#)
int[] arr = {1, 2, 3};
var list = new List<int>(arr);
list.Add(4);
Console.WriteLine(arr.Length);
Console.WriteLine(list.Count);
A3\n4
B3\n3
C4\n4
D4\n3
Attempts:
2 left
💡 Hint
Remember that arrays have fixed size, but Lists can grow.
🧠 Conceptual
intermediate
1:30remaining
Why choose collections over arrays?
Which of the following is the main reason to prefer collections like List over arrays in C#?
AArrays allow adding elements after creation, collections do not.
BArrays support LINQ queries, collections do not.
CCollections are always faster than arrays.
DCollections can dynamically resize, arrays have fixed size.
Attempts:
2 left
💡 Hint
Think about flexibility in size.
Predict Output
advanced
2:00remaining
Output of modifying collection vs array reference
What will be the output of this C# code?
C Sharp (C#)
int[] arr = {10, 20, 30};
List<int> list = new List<int>(arr);
arr[0] = 100;
list[1] = 200;
Console.WriteLine(arr[0]);
Console.WriteLine(list[1]);
A100\n200
B10\n200
C10\n20
D100\n20
Attempts:
2 left
💡 Hint
Modifying the array changes its own elements; modifying the list changes its own copy.
🔧 Debug
advanced
1:30remaining
Identify the error when resizing an array
What error will this C# code produce?
C Sharp (C#)
int[] arr = new int[3];
arr[3] = 10;
ANullReferenceException
BIndexOutOfRangeException
CSyntaxError
DNo error, runs fine
Attempts:
2 left
💡 Hint
Array indices start at 0 and go up to length-1.
🚀 Application
expert
2:30remaining
Choosing the right data structure for dynamic data
You need to store a list of user names that can grow and shrink during program execution. Which is the best choice in C#?
AUse a string[] array because it is faster to access.
BUse a Dictionary<int, string> to store user names by index.
CUse a List<string> because it can dynamically resize.
DUse a fixed size array and create a new one when resizing is needed.
Attempts:
2 left
💡 Hint
Think about ease of adding and removing elements.