Challenge - 5 Problems
Computed Properties Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of computed property in C#
What is the output of this C# program that uses a computed property?
C Sharp (C#)
public class Rectangle { public int Width { get; set; } public int Height { get; set; } public int Area => Width * Height; } class Program { static void Main() { var rect = new Rectangle { Width = 4, Height = 5 }; System.Console.WriteLine(rect.Area); } }
Attempts:
2 left
💡 Hint
The Area property calculates Width multiplied by Height.
✗ Incorrect
The computed property Area returns Width * Height, which is 4 * 5 = 20.
❓ Predict Output
intermediate2:00remaining
Computed property with backing field
What will be the output of this C# code using a computed property with a backing field?
C Sharp (C#)
public class Circle { private double radius; public double Radius { get => radius; set => radius = value; } public double Diameter => 2 * Radius; } class Program { static void Main() { var c = new Circle { Radius = 3 }; System.Console.WriteLine(c.Diameter); } }
Attempts:
2 left
💡 Hint
Diameter is twice the radius.
✗ Incorrect
Diameter returns 2 times Radius, so 2 * 3 = 6.
❓ Predict Output
advanced2:00remaining
Computed property with conditional logic
What is the output of this C# program with a computed property that uses conditional logic?
C Sharp (C#)
public class Temperature { public double Celsius { get; set; } public string State => Celsius >= 100 ? "Gas" : Celsius <= 0 ? "Solid" : "Liquid"; } class Program { static void Main() { var t = new Temperature { Celsius = 50 }; System.Console.WriteLine(t.State); } }
Attempts:
2 left
💡 Hint
Check the temperature ranges for state.
✗ Incorrect
Since 50 is between 0 and 100, State returns "Liquid".
❓ Predict Output
advanced2:00remaining
Computed property with side effect
What will be printed by this C# program where a computed property has a side effect?
C Sharp (C#)
public class Counter { private int count = 0; public int Count { get { count++; return count; } } } class Program { static void Main() { var c = new Counter(); System.Console.WriteLine(c.Count); System.Console.WriteLine(c.Count); } }
Attempts:
2 left
💡 Hint
Each access to Count increments count before returning.
✗ Incorrect
The first call returns 1, the second returns 2 because count increments each time.
🧠 Conceptual
expert2:00remaining
Understanding computed properties behavior
Which statement about computed properties in C# is TRUE?
Attempts:
2 left
💡 Hint
Think about when the code inside a computed property runs.
✗ Incorrect
Computed properties run their code each time they are accessed; they do not store values unless explicitly coded.