public int Total { get { return Price * Quantity; } } uses a property with only a get accessor returning a calculation, which is correct.
Step 2: Check other options for errors
public int Total { set { Price = value; } } only has set, so not read-only. public int Total => Price + Quantity { get; set; } mixes expression body with get/set incorrectly. public int Total() { return Price + Quantity; } is a method, not a property.
Final Answer:
public int Total { get { return Price * Quantity; } } -> Option B
Quick Check:
Read-only computed property = get only [OK]
Hint: Read-only properties have only get accessor [OK]
Common Mistakes:
Using set accessor in read-only properties
Confusing methods with properties
Incorrect expression body syntax
3. What is the output of this code?
class Box {
public int Width { get; set; } = 3;
public int Height { get; set; } = 4;
public int Area => Width * Height;
}
var box = new Box();
Console.WriteLine(box.Area);
medium
A. 12
B. 7
C. 0
D. Error
Solution
Step 1: Identify property values
Width is 3 and Height is 4 as set by default.
Step 2: Calculate computed property Area
Area returns Width * Height = 3 * 4 = 12.
Final Answer:
12 -> Option A
Quick Check:
3 * 4 = 12 [OK]
Hint: Multiply Width and Height for Area [OK]
Common Mistakes:
Adding instead of multiplying
Assuming default values are zero
Confusing property with method call
4. Find the error in this computed property code:
public class Circle {
public double Radius { get; set; }
public double Diameter => Radius * 2;
public double Circumference { get { return 2 * Math.PI * Diameter; } set { Diameter = value; } }
}
medium
A. Circumference should not use Math.PI
B. Radius property is missing get accessor
C. Computed property Diameter cannot be assigned in set accessor
D. No error, code is correct
Solution
Step 1: Check computed property Diameter
Diameter is read-only computed property with only get accessor (=>). It cannot be assigned a value.
Step 2: Analyze Circumference set accessor
Circumference tries to set Diameter, which is not allowed because Diameter has no set accessor.
Final Answer:
Computed property Diameter cannot be assigned in set accessor -> Option C
Quick Check:
Read-only property cannot be set [OK]
Hint: Computed properties with => are read-only [OK]
Common Mistakes:
Trying to set read-only computed properties
Ignoring property accessors
Confusing methods with properties
5. You want to create a computed property IsAdult in a Person class that returns true if Age is 18 or more, otherwise false. Which code correctly implements this?
hard
A. public bool IsAdult() { return Age >= 18; }
B. public bool IsAdult { get { return Age > 18; } }
C. public bool IsAdult { get; set; } = Age >= 18;
D. public bool IsAdult => Age >= 18;
Solution
Step 1: Understand requirement for computed property
IsAdult should return true if Age is 18 or more, false otherwise, without storing a value.
Step 2: Check each option
public bool IsAdult => Age >= 18; uses expression-bodied property correctly with >= 18. public bool IsAdult { get { return Age > 18; } } uses > 18 (wrong condition). public bool IsAdult { get; set; } = Age >= 18; tries to set property with Age comparison, which is invalid. public bool IsAdult() { return Age >= 18; } is a method, not a property.
Final Answer:
public bool IsAdult => Age >= 18; -> Option D
Quick Check:
Age >= 18 for IsAdult [OK]
Hint: Use => with condition for simple computed properties [OK]