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

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

Choose your learning style10 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.

Practice

(1/5)
1. What happens when you assign one reference type variable to another in C#?
ClassA a = new ClassA();
ClassA b = a;
easy
A. Both variables point to the same object in memory.
B. A new copy of the object is created for b.
C. The variables become independent and do not affect each other.
D. The program throws a compile-time error.

Solution

  1. Step 1: Understand reference assignment

    Assigning one reference variable to another makes both variables point to the same object in memory.
  2. Step 2: Effect of shared reference

    Changes made through either variable affect the same object, so both see the changes.
  3. Final Answer:

    Both variables point to the same object in memory. -> Option A
  4. Quick Check:

    Reference assignment = shared object [OK]
Hint: Reference variables share the same object after assignment [OK]
Common Mistakes:
  • Thinking a new object is created on assignment
  • Assuming variables become independent
  • Confusing value types with reference types
2. Which of the following is the correct syntax to assign one reference variable to another in C#?
easy
A. int a = b;
B. ClassA a := b;
C. ClassA a == b;
D. ClassA a = new ClassA(); a = b;

Solution

  1. Step 1: Identify correct assignment operator

    In C#, the single equals sign (=) is used for assignment, not double equals (==) or :=.
  2. Step 2: Check variable types and initialization

    ClassA a = new ClassA(); a = b; correctly declares a reference variable and assigns another reference variable to it.
  3. Final Answer:

    ClassA a = new ClassA(); a = b; -> Option D
  4. Quick Check:

    Use = for assignment in C# [OK]
Hint: Use single = for assignment, not == or := [OK]
Common Mistakes:
  • Using == instead of = for assignment
  • Using := which is invalid in C#
  • Assigning incompatible types without casting
3. What is the output of the following code?
class Box { public int Value; }
Box box1 = new Box();
box1.Value = 5;
Box box2 = box1;
box2.Value = 10;
Console.WriteLine(box1.Value);
medium
A. 5
B. 0
C. 10
D. Compilation error

Solution

  1. Step 1: Understand reference assignment effect

    box2 is assigned to box1, so both point to the same Box object.
  2. Step 2: Trace value changes

    Changing box2.Value to 10 changes the shared object, so box1.Value is also 10.
  3. Final Answer:

    10 -> Option C
  4. Quick Check:

    Shared reference means shared data [OK]
Hint: Changing one reference changes all pointing to same object [OK]
Common Mistakes:
  • Assuming box1.Value stays 5 after box2 changes it
  • Confusing reference and value types
  • Expecting a compile error due to assignment
4. Identify the error in this code snippet:
class Person { public string Name; }
Person p1 = new Person();
p1.Name = "Alice";
Person p2;
p2.Name = "Bob";
p1 = p2;
medium
A. p2 is used before being assigned an object.
B. Cannot assign p2 to p1 because they are different types.
C. Cannot assign string to Name property.
D. No error, code runs fine.

Solution

  1. Step 1: Check variable initialization

    p2 is declared but not assigned an object before accessing p2.Name.
  2. Step 2: Understand runtime error

    Accessing p2.Name causes a NullReferenceException because p2 is null.
  3. Final Answer:

    p2 is used before being assigned an object. -> Option A
  4. Quick Check:

    Uninitialized reference causes runtime error [OK]
Hint: Always initialize reference variables before use [OK]
Common Mistakes:
  • Assuming p2 has default object automatically
  • Confusing compile-time and runtime errors
  • Ignoring null reference exceptions
5. Given this code, what will be the output?
class Data { public int Number; }
Data d1 = new Data() { Number = 1 };
Data d2 = new Data() { Number = 2 };
d1 = d2;
d2.Number = 5;
Console.WriteLine(d1.Number);
hard
A. 2
B. 5
C. 1
D. Compilation error

Solution

  1. Step 1: Analyze reference assignment

    d1 is assigned to d2, so both point to the same object with Number initially 2.
  2. Step 2: Trace value change through d2

    Changing d2.Number to 5 changes the shared object, so d1.Number is also 5.
  3. Final Answer:

    5 -> Option B
  4. Quick Check:

    Reference assignment shares object state [OK]
Hint: After assignment, variables share the same object state [OK]
Common Mistakes:
  • Thinking d1.Number remains 1 after assignment
  • Confusing value and reference types
  • Expecting compile errors on assignment
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard