Challenge - 5 Problems
Collection Initialization Master
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# 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 };
Attempts:
2 left
💡 Hint
Look at the values inside the curly braces after the list declaration.
✗ Incorrect
The list is initialized with the values 1 through 5 in order, so the list contains [1, 2, 3, 4, 5].
📝 Syntax
intermediate2: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?
Attempts:
2 left
💡 Hint
Dictionary initialization uses curly braces with key-value pairs inside another set of braces.
✗ Incorrect
The correct syntax for initializing a dictionary uses curly braces with each key-value pair enclosed in its own braces: { {key, value}, ... }.
❓ optimization
advanced2: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?
Attempts:
2 left
💡 Hint
Consider built-in methods that generate sequences efficiently.
✗ Incorrect
Using Enumerable.Range with the List constructor creates the list in one step efficiently. Manually adding or typing all values is less efficient.
🔧 Debug
advanced2: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?
Attempts:
2 left
💡 Hint
Think about how HashSet handles duplicates.
✗ Incorrect
HashSet automatically ignores duplicate values, so the set contains unique values only without errors.
🧠 Conceptual
expert2:00remaining
Which statement about collection initializers in C# is true?
Select the correct statement about collection initializers in C#.
Attempts:
2 left
💡 Hint
Think about what the compiler uses to add items during initialization.
✗ Incorrect
Collection initializers work by calling the Add method on the collection for each item.