Bird
Raised Fist0
C Sharp (C#)programming~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does collection initialization syntax in C# allow you to do?
var list = new List<int> { 1, 2, 3 };
easy
A. Only create an empty collection
B. Create and fill a collection in one step
C. Fill a collection after creating it separately
D. Create a collection without specifying type

Solution

  1. Step 1: Understand collection initialization

    Collection initialization lets you create a collection and add items to it in one line.
  2. Step 2: Analyze the example code

    The code new List<int> { 1, 2, 3 } creates a list and fills it with 1, 2, and 3 immediately.
  3. Final Answer:

    Create and fill a collection in one step -> Option B
  4. Quick Check:

    Collection initialization = create + fill [OK]
Hint: Remember: initialization means create and fill together [OK]
Common Mistakes:
  • Thinking it only creates empty collections
  • Confusing initialization with adding items later
  • Assuming type is optional in initialization
2. Which of the following is the correct syntax to initialize a dictionary with keys and values in C#?
easy
A. var dict = new Dictionary { 1: "One", 2: "Two" };
B. var dict = new Dictionary (1: "One", 2: "Two");
C. var dict = new Dictionary { (1, "One"), (2, "Two") };
D. var dict = new Dictionary { {1, "One"}, {2, "Two"} };

Solution

  1. Step 1: Recall dictionary initialization syntax

    Dictionaries use curly braces with key-value pairs inside another set of braces: { {key, value}, ... }.
  2. Step 2: Check each option

    var dict = new Dictionary { {1, "One"}, {2, "Two"} }; uses correct syntax with nested braces for each pair. Others use invalid syntax for C# dictionaries.
  3. Final Answer:

    var dict = new Dictionary { {1, "One"}, {2, "Two"} }; -> Option D
  4. Quick Check:

    Dictionary init = nested braces for pairs [OK]
Hint: Use double braces for dictionary key-value pairs [OK]
Common Mistakes:
  • Using colon instead of comma between key and value
  • Trying to use parentheses instead of braces
  • Confusing dictionary syntax with object initializers
3. What will be the output of the following C# code?
var set = new HashSet<int> { 1, 2, 2, 3 };
Console.WriteLine(set.Count);
medium
A. 3
B. Runtime error
C. 2
D. 4

Solution

  1. Step 1: Understand HashSet behavior

    A HashSet stores unique elements only, so duplicates are ignored.
  2. Step 2: Analyze the initialization

    The set is initialized with {1, 2, 2, 3}, but the duplicate '2' is ignored, so the set contains 3 unique elements.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    HashSet ignores duplicates = count 3 [OK]
Hint: HashSet counts unique items only [OK]
Common Mistakes:
  • Counting duplicates as separate elements
  • Expecting a runtime error due to duplicates
  • Confusing HashSet with List behavior
4. Identify the error in this collection initialization:
var list = new List<int> { 1, 2, "3" };
medium
A. Using parentheses instead of braces
B. Missing semicolon at the end
C. Mixing int and string types in List<int>
D. List cannot be initialized with values

Solution

  1. Step 1: Check the List type and values

    The list is declared as List<int>, so all elements must be integers.
  2. Step 2: Identify the incorrect value

    The value "3" is a string, not an int, causing a type mismatch error.
  3. Final Answer:

    Mixing int and string types in List<int> -> Option C
  4. Quick Check:

    List<int> must contain only ints [OK]
Hint: All items must match the collection's declared type [OK]
Common Mistakes:
  • Assuming implicit conversion from string to int
  • Ignoring type mismatch errors
  • Thinking semicolon or braces are the problem
5. You want to initialize a dictionary where keys are strings and values are lists of integers. Which is the correct way to do this in C#?
hard
A. var dict = new Dictionary { { "a", new List<int> {1, 2} }, { "b", new List<int> {3, 4} } };
B. var dict = new Dictionary { { "a", {1, 2} }, { "b", {3, 4} } };
C. var dict = new Dictionary { ( "a", [1, 2] ), ( "b", [3, 4] ) };
D. var dict = new Dictionary { "a": [1, 2], "b": [3, 4] };

Solution

  1. Step 1: Understand nested collection initialization

    Each dictionary value is a List<int>, so you must create new List<int> instances inside the dictionary initializer.
  2. Step 2: Check syntax correctness

    var dict = new Dictionary { { "a", new List<int> {1, 2} }, { "b", new List<int> {3, 4} } }; correctly uses nested initializers: dictionary with key-value pairs, where values are new List<int> with their own initializers.
  3. Final Answer:

    var dict = new Dictionary { { "a", new List<int> {1, 2} }, { "b", new List<int> {3, 4} } }; -> Option A
  4. Quick Check:

    Nested collections need explicit new List<int> [OK]
Hint: Use 'new' for nested collections inside dictionary [OK]
Common Mistakes:
  • Omitting 'new List<int>' for nested lists
  • Using parentheses or brackets incorrectly
  • Trying to use colon syntax inside C# initializers