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

Computed properties in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Computed Properties Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
    }
}
A20
B9
C0
DCompilation error
Attempts:
2 left
💡 Hint
The Area property calculates Width multiplied by Height.
Predict Output
intermediate
2: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);
    }
}
A3
B9
CRuntime error
D6
Attempts:
2 left
💡 Hint
Diameter is twice the radius.
Predict Output
advanced
2: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);
    }
}
A"Solid"
B"Gas"
C"Liquid"
DCompilation error
Attempts:
2 left
💡 Hint
Check the temperature ranges for state.
Predict Output
advanced
2: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);
    }
}
A1\n2
BCompilation error
C1\n1
D0\n1
Attempts:
2 left
💡 Hint
Each access to Count increments count before returning.
🧠 Conceptual
expert
2:00remaining
Understanding computed properties behavior
Which statement about computed properties in C# is TRUE?
AComputed properties always cache their result after first calculation.
BComputed properties execute their code every time they are accessed.
CComputed properties cannot use conditional logic.
DComputed properties can store values internally without a backing field.
Attempts:
2 left
💡 Hint
Think about when the code inside a computed property runs.