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

Common bugs from reference sharing in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Reference Sharing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this code with shared list references?

Consider the following C# code where two variables reference the same list. What will be printed?

C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        List<int> listA = new List<int> {1, 2, 3};
        List<int> listB = listA;
        listB.Add(4);
        Console.WriteLine(string.Join(",", listA));
    }
}
A1,2,3,4
B1,2,3
C4
DCompilation error
Attempts:
2 left
💡 Hint

Think about what happens when two variables point to the same list and one changes it.

❓ Predict Output
intermediate
2:00remaining
What happens when modifying a shared object property?

Look at this C# code where two variables reference the same object. What will be printed?

C Sharp (C#)
using System;

class Person {
    public string Name;
}

class Program {
    static void Main() {
        Person p1 = new Person { Name = "Alice" };
        Person p2 = p1;
        p2.Name = "Bob";
        Console.WriteLine(p1.Name);
    }
}
AAlice
BBob
Cnull
DCompilation error
Attempts:
2 left
💡 Hint

Remember that objects are reference types in C#.

❓ Predict Output
advanced
2:00remaining
What is the output when modifying a shared array inside a method?

What will this C# program print?

C Sharp (C#)
using System;

class Program {
    static void ModifyArray(int[] arr) {
        arr[0] = 100;
    }

    static void Main() {
        int[] numbers = {1, 2, 3};
        ModifyArray(numbers);
        Console.WriteLine(string.Join(",", numbers));
    }
}
A1,2,3
BCompilation error
C0,2,3
D100,2,3
Attempts:
2 left
💡 Hint

Arrays are reference types in C#. What happens when you change an element inside a method?

❓ Predict Output
advanced
2:00remaining
What is the output when cloning a list incorrectly?

What will this C# code print?

C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        List<int> original = new List<int> {1, 2, 3};
        List<int> clone = original;
        clone.Add(4);
        Console.WriteLine(string.Join(",", original));
    }
}
A1,2,3
B4
C1,2,3,4
DCompilation error
Attempts:
2 left
💡 Hint

Assigning one list to another copies the reference, not the list itself.

❓ Predict Output
expert
2:00remaining
What is the output when modifying a shared dictionary value?

Consider this C# code. What will be printed?

C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        var dict1 = new Dictionary<string, List<int>>();
        dict1["key"] = new List<int> {1, 2};
        var dict2 = dict1;
        dict2["key"].Add(3);
        Console.WriteLine(string.Join(",", dict1["key"]));
    }
}
A1,2,3
B1,2
C3
DKeyNotFoundException
Attempts:
2 left
💡 Hint

Dictionaries and lists are reference types. What happens when you modify a list inside a shared dictionary?

Practice

(1/5)
1. What happens when you assign one object variable to another in C#?
easy
A. Both variables refer to the same object in memory.
B. A new independent copy of the object is created automatically.
C. The original object is deleted and replaced by the new variable.
D. The variables become unrelated and hold different objects.

Solution

  1. Step 1: Understand object assignment in C#

    Assigning one object variable to another copies the reference, not the object itself.
  2. Step 2: Effect of reference copying

    Both variables point to the same object in memory, so changes via one affect the other.
  3. Final Answer:

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

    Reference copy = same object [OK]
Hint: Assigning objects copies references, not objects [OK]
Common Mistakes:
  • Thinking a new object is created automatically
  • Assuming variables hold independent objects
  • Confusing value types with reference types
2. Which of the following is the correct way to create a copy of an object to avoid reference sharing in C#?
easy
A. Use a method like MemberwiseClone() or implement ICloneable.
B. Use the assignment operator (=) between two object variables.
C. Declare the object variable as readonly.
D. Use the 'new' keyword without copying fields.

Solution

  1. Step 1: Identify how to copy objects in C#

    Assignment copies references, so to create a copy, you need cloning methods like MemberwiseClone() or ICloneable.
  2. Step 2: Why other options are incorrect

    Assignment operator copies reference; readonly only prevents reassignment; 'new' without copying fields creates a new empty object.
  3. Final Answer:

    Use a method like MemberwiseClone() or implement ICloneable. -> Option A
  4. Quick Check:

    Clone method = copy object [OK]
Hint: Clone objects to avoid shared references [OK]
Common Mistakes:
  • Using assignment operator expecting a copy
  • Confusing readonly with copying
  • Creating new object without copying data
3. What will be the output of the following C# code?
class Box { public int Value; }
var box1 = new Box { Value = 5 };
var box2 = box1;
box2.Value = 10;
Console.WriteLine(box1.Value);
medium
A. 5
B. Compilation error
C. 0
D. 10

Solution

  1. Step 1: Understand reference assignment in the code

    box2 is assigned box1, so both refer to the same Box object.
  2. Step 2: Effect of changing box2.Value

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

    10 -> Option D
  4. Quick Check:

    Shared reference means value changes reflect [OK]
Hint: Changing one reference changes all pointing to same object [OK]
Common Mistakes:
  • Expecting box1.Value to remain 5
  • Confusing value and reference types
  • Assuming box2 is a separate object
4. Identify the bug in this C# code snippet related to reference sharing:
class Person { public string Name; }
var p1 = new Person { Name = "Alice" };
var p2 = p1;
p2.Name = "Bob";
Console.WriteLine(p1.Name);
medium
A. p1 and p2 refer to different objects, so no bug.
B. p2.Name change affects p1.Name because both reference the same object.
C. The code causes a null reference exception.
D. The assignment operator creates a deep copy automatically.

Solution

  1. Step 1: Analyze object references in the code

    p2 is assigned p1, so both variables point to the same Person object.
  2. Step 2: Effect of modifying p2.Name

    Changing p2.Name to "Bob" changes the shared object, so p1.Name also becomes "Bob".
  3. Final Answer:

    p2.Name change affects p1.Name because both reference the same object. -> Option B
  4. Quick Check:

    Shared reference causes shared changes [OK]
Hint: Check if variables share the same object reference [OK]
Common Mistakes:
  • Assuming p1.Name remains "Alice"
  • Expecting automatic deep copy on assignment
  • Thinking null reference exception occurs
5. You want to create two independent lists of integers in C# so that modifying one list does not affect the other. Which code snippet correctly avoids reference sharing?
hard
A. var list1 = new List<int> {1, 2, 3}; var list2 = list1; list2.Add(4);
B. var list1 = new List<int> {1, 2, 3}; var list2 = list1; list1.Clear();
C. var list1 = new List<int> {1, 2, 3}; var list2 = new List<int>(list1); list2.Add(4);
D. var list1 = new List<int> {1, 2, 3}; var list2 = list1; list1 = new List<int>();

Solution

  1. Step 1: Understand reference sharing with lists

    Assigning list2 = list1 copies the reference, so both variables point to the same list.
  2. Step 2: Creating an independent copy

    Using new List<int>(list1) creates a new list copying elements, so list2 is independent.
  3. Final Answer:

    var list2 = new List<int>(list1); list2.Add(4); -> Option C
  4. Quick Check:

    New list constructor copies elements [OK]
Hint: Use new List<T>(existingList) to copy lists [OK]
Common Mistakes:
  • Assigning list2 = list1 expecting copy
  • Clearing list1 expecting list2 unaffected
  • Reassigning list1 does not affect list2 reference
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