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

Reference assignment and shared state in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to assign the list to another variable.

C Sharp (C#)
List<int> numbers = new List<int> {1, 2, 3};
List<int> copy = [1];
Drag options to blanks, or click blank then click option'
Anull
Bnew List<int>()
Cnumbers
Dcopy
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'new List()' creates a new empty list instead of referencing the existing one.
Assigning 'null' breaks the reference.
2fill in blank
medium

Complete the code to add an item to the list through the second reference.

C Sharp (C#)
List<string> fruits = new List<string> {"apple", "banana"};
List<string> basket = fruits;
basket.[1]("orange");
Drag options to blanks, or click blank then click option'
ARemove
BAdd
CClear
DInsert
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Remove' deletes items instead of adding.
Using 'Clear' empties the list.
3fill in blank
hard

Fix the error in the code to correctly copy the list without shared state.

C Sharp (C#)
List<int> original = new List<int> {1, 2, 3};
List<int> copy = original.[1]();
Drag options to blanks, or click blank then click option'
AClone
BCopyTo
CClear
DToList
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Clone' is not available for List.
Using 'CopyTo' copies elements to an array, not a new list.
4fill in blank
hard

Fill both blanks to create a dictionary with word lengths for words longer than 3 characters.

C Sharp (C#)
var words = new List<string> {"cat", "house", "tree", "a"};
var lengths = words.Where(w => w.[1] > 3)
    .ToDictionary(w => w, w => w.[2]);
Drag options to blanks, or click blank then click option'
ALength
BCount
CLength()
DCount()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Count' or 'Count()' which are for collections, not strings.
Using 'Length()' which is invalid because Length is not a method.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values for words longer than 2 characters.

C Sharp (C#)
var words = new List<string> {"dog", "cat", "bird", "a"};
var result = words.Where(w => w.[1] > 2)
    .ToDictionary(w => w.[2](), w => w.[3]());
Drag options to blanks, or click blank then click option'
ALength
BToUpper
CToLower
DCount
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Count' instead of 'Length' for string length.
Using 'ToLower()' instead of 'ToUpper()' for uppercase conversion.