Challenge - 5 Problems
Property Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a read-only property
What is the output of this C# code snippet?
C Sharp (C#)
class Person { private string name = "Alice"; public string Name { get { return name; } } } var p = new Person(); Console.WriteLine(p.Name);
Attempts:
2 left
💡 Hint
Look at the get accessor and the initial value of the private field.
✗ Incorrect
The property 'Name' has only a get accessor returning the private field 'name' which is initialized to "Alice". So printing p.Name outputs "Alice".
❓ Predict Output
intermediate2:00remaining
Effect of write-only property
What happens when you try to read a write-only property in this code?
C Sharp (C#)
class Secret { private string code; public string Code { set { code = value; } } } var s = new Secret(); s.Code = "1234"; Console.WriteLine(s.Code);
Attempts:
2 left
💡 Hint
Check if the property has a get accessor.
✗ Incorrect
The property 'Code' has only a set accessor and no get accessor. Trying to read s.Code causes a compilation error.
🧠 Conceptual
advanced2:00remaining
Understanding read-only auto-properties
Which statement about this code is true?
C Sharp (C#)
class Data { public int Value { get; } = 10; } var d = new Data(); d.Value = 20;
Attempts:
2 left
💡 Hint
Check if the property allows assignment outside the constructor.
✗ Incorrect
The property 'Value' has only a get accessor and is initialized inline. Assigning to it outside the constructor causes a compilation error.
❓ Predict Output
advanced2:00remaining
Behavior of write-only property with backing field
What is the output of this code?
C Sharp (C#)
class Box { private int size = 5; public int Size { set { size = value; } } public int GetSize() => size; } var b = new Box(); b.Size = 10; Console.WriteLine(b.GetSize());
Attempts:
2 left
💡 Hint
The property sets the private field, and GetSize returns it.
✗ Incorrect
The write-only property 'Size' sets the private field 'size'. After setting b.Size = 10, calling GetSize returns 10.
🔧 Debug
expert3:00remaining
Why does this read-only property cause a compilation error?
Identify the cause of the compilation error in this code snippet.
C Sharp (C#)
class Counter { private int count; public int Count { get { return count; } } public void Increment() { Count++; } }
Attempts:
2 left
💡 Hint
Consider what happens when Count++ is executed with this property implementation.
✗ Incorrect
The property 'Count' has only a get accessor (read-only). Count++ requires both get and set accessors: it reads the value then assigns the incremented value back. Without a set accessor, it causes a compilation error.