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

Get and set accessors in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Accessor Mastery
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 C# code using get and set accessors?

Consider the following C# class with a property using get and set accessors. What will be printed when the program runs?

C Sharp (C#)
class Person {
    private string name;
    public string Name {
        get { return name; }
        set { name = value.ToUpper(); }
    }
}

class Program {
    static void Main() {
        Person p = new Person();
        p.Name = "alice";
        System.Console.WriteLine(p.Name);
    }
}
Anull
Balice
CAlice
DALICE
Attempts:
2 left
💡 Hint

Look at how the set accessor modifies the value before storing it.

Predict Output
intermediate
2:00remaining
What value does the property return after setting it?

Given this C# code, what will be the output?

C Sharp (C#)
class Counter {
    private int count;
    public int Count {
        get { return count; }
        set {
            if (value >= 0) count = value;
        }
    }
}

class Program {
    static void Main() {
        Counter c = new Counter();
        c.Count = -5;
        System.Console.WriteLine(c.Count);
    }
}
A-5
B0
CThrows an exception
DUninitialized value
Attempts:
2 left
💡 Hint

Check what happens when the set accessor receives a negative value.

🔧 Debug
advanced
2:00remaining
Why does this property cause a stack overflow error?

Examine the following C# property. Why does it cause a runtime error?

C Sharp (C#)
class Sample {
    private int number;
    public int Number {
        get { return Number; }
        set { Number = value; }
    }
}
AThe get and set accessors recursively call themselves causing a stack overflow.
BThe private field 'number' is not initialized causing a null reference exception.
CThe property is missing a semicolon causing a syntax error.
DThe property uses an invalid data type causing a compile error.
Attempts:
2 left
💡 Hint

Look at what the get and set accessors are returning and assigning.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a read-only property with a private set accessor?

Choose the correct C# property syntax that allows reading the value publicly but only setting it privately within the class.

Apublic int Age { get private; set; }
Bpublic int Age { private get; set; }
Cpublic int Age { get; private set; }
Dpublic int Age { get; set; private }
Attempts:
2 left
💡 Hint

Remember the correct order and syntax for access modifiers on accessors.

🚀 Application
expert
3:00remaining
How many items are in the dictionary after this code runs?

Analyze this C# code using a property with get and set accessors that modify a dictionary. How many key-value pairs does the dictionary contain after execution?

C Sharp (C#)
class DataStore {
    public Dictionary<string, int> data = new();
    public int this[string key] {
        get => data.ContainsKey(key) ? data[key] : -1;
        set {
            if (value >= 0) data[key] = value;
            else if (data.ContainsKey(key)) data.Remove(key);
        }
    }
}

class Program {
    static void Main() {
        DataStore store = new DataStore();
        store["a"] = 10;
        store["b"] = 20;
        store["a"] = -5;
        store["c"] = 0;
        store["b"] = -1;
        System.Console.WriteLine(store.data.Count);
    }
}
A1
B2
C3
D0
Attempts:
2 left
💡 Hint

Track how the set accessor adds, updates, or removes keys based on the value.