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

Collection initialization syntax in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Collection Initialization Master
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# collection initialization?
Consider the following C# code snippet that initializes a list of integers. What will be the content of the list after initialization?
C Sharp (C#)
var numbers = new List<int> { 1, 2, 3, 4, 5 };
A[1, 2, 3, 4, 5]
B[5, 4, 3, 2, 1]
C[]
D[0, 1, 2, 3, 4]
Attempts:
2 left
💡 Hint
Look at the values inside the curly braces after the list declaration.
📝 Syntax
intermediate
2:00remaining
Which option correctly initializes a dictionary with string keys and int values?
You want to create a dictionary in C# with keys "apple", "banana", and "cherry" and values 1, 2, and 3 respectively. Which of the following collection initializations is correct?
Avar dict = new Dictionary<string, int> { ["apple"] = 1, ["banana"] = 2, ["cherry"] = 3 };
Bvar dict = new Dictionary<string, int> { ("apple", 1), ("banana", 2), ("cherry", 3) };
Cvar dict = new Dictionary<string, int> { "apple": 1, "banana": 2, "cherry": 3 };
Dvar dict = new Dictionary<string, int> { {"apple", 1}, {"banana", 2}, {"cherry", 3} };
Attempts:
2 left
💡 Hint
Dictionary initialization uses curly braces with key-value pairs inside another set of braces.
optimization
advanced
2:00remaining
Which initialization is more efficient for a large list of integers?
You need to initialize a list of 1000 integers from 1 to 1000 in C#. Which option is more efficient in terms of performance and memory?
Avar list = new List<int>(Enumerable.Range(1, 1000));
Bvar list = new List<int>(); list = Enumerable.Range(1, 1000).ToList();
Cvar list = new List<int> { 1, 2, 3, ..., 1000 }; // manually typed
Dvar list = new List<int>(); for(int i=1; i<=1000; i++) { list.Add(i); }
Attempts:
2 left
💡 Hint
Consider built-in methods that generate sequences efficiently.
🔧 Debug
advanced
2:00remaining
What error does this collection initialization cause?
Examine the following C# code snippet: var set = new HashSet { 1, 2, 3, 2, 4 }; What happens when this code runs?
ACompilation error due to duplicate values in initialization.
BThe HashSet contains {1, 2, 3, 4} with no error.
CRuntime error due to duplicate values.
DThe HashSet contains {1, 2, 3, 2, 4} including duplicates.
Attempts:
2 left
💡 Hint
Think about how HashSet handles duplicates.
🧠 Conceptual
expert
2:00remaining
Which statement about collection initializers in C# is true?
Select the correct statement about collection initializers in C#.
ACollection initializers automatically sort the collection after initialization.
BCollection initializers can only be used with arrays and lists.
CCollection initializers require the collection to have an Add method.
DCollection initializers can only add primitive types to collections.
Attempts:
2 left
💡 Hint
Think about what the compiler uses to add items during initialization.