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

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

Choose your learning style9 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?