Bird
Raised Fist0
Swiftprogramming~20 mins

Computed properties in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift Computed Properties Master
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 Swift code using computed properties?
Consider the following Swift struct with a computed property. What will be printed when the code runs?
Swift
struct Rectangle {
    var width: Double
    var height: Double
    var area: Double {
        return width * height
    }
}

let rect = Rectangle(width: 3, height: 4)
print(rect.area)
A12.0
B7.0
C0.0
DError: Cannot access property
Attempts:
2 left
💡 Hint
Remember that computed properties calculate their value when accessed.
❓ Predict Output
intermediate
2:00remaining
What value does the computed property return after mutation?
Given this Swift class with a computed property, what will be printed after changing the base value?
Swift
class Circle {
    var radius: Double
    var diameter: Double {
        get {
            return radius * 2
        }
        set {
            radius = newValue / 2
        }
    }
    init(radius: Double) {
        self.radius = radius
    }
}

let c = Circle(radius: 5)
c.diameter = 20
print(c.radius)
A10.0
B5.0
C20.0
DError: Cannot assign to computed property
Attempts:
2 left
💡 Hint
Setting diameter changes radius because of the setter.
❓ Predict Output
advanced
2:00remaining
What is the output of this Swift code with a computed property using a private stored property?
Analyze the following Swift struct. What will be printed when accessing the computed property 'fullName'?
Swift
struct Person {
    private var firstName: String
    private var lastName: String
    var fullName: String {
        return "\(firstName) \(lastName)"
    }
    init(firstName: String, lastName: String) {
        self.firstName = firstName
        self.lastName = lastName
    }
}

let p = Person(firstName: "Jane", lastName: "Doe")
print(p.fullName)
AJane
BfirstName lastName
CJane Doe
DError: Cannot access private property
Attempts:
2 left
💡 Hint
Computed properties can access private stored properties within the same struct.
❓ Predict Output
advanced
2:00remaining
What error does this Swift code produce when using a computed property incorrectly?
Examine the following Swift code. What error will occur when compiling?
Swift
struct Square {
    var side: Double
    var area: Double {
        side * side
    }
    var perimeter: Double {
        return side * 4
    }
    var invalidComputed: Double {
        side * 2
        side * 2
    }
}
AError: Cannot use expression without return in computed property
BError: Duplicate computed property name
CNo error, code compiles successfully
DError: Missing return in computed property 'invalidComputed'
Attempts:
2 left
💡 Hint
Computed properties without explicit return require single expression bodies.
🧠 Conceptual
expert
2:00remaining
How many times is the computed property 'value' accessed in this Swift code?
Consider this Swift struct with a computed property. How many times will the computed property 'value' be evaluated during the execution of the code below?
Swift
struct Counter {
    var base: Int
    var value: Int {
        print("Computing value")
        return base * 2
    }
}

let c = Counter(base: 3)
let sum = c.value + c.value + c.value
print(sum)
A1 time
B3 times
C0 times
D2 times
Attempts:
2 left
💡 Hint
Each access to a computed property runs its code anew unless cached.

Practice

(1/5)
1. What is a computed property in Swift?
easy
A. A property that can only be set but not read
B. A property that stores a fixed value permanently
C. A property that calculates its value dynamically when accessed
D. A property that is declared outside a class or struct

Solution

  1. Step 1: Understand property types in Swift

    Stored properties hold fixed values, while computed properties calculate values dynamically.
  2. Step 2: Identify computed property behavior

    Computed properties use code to return a value each time they are accessed instead of storing it.
  3. Final Answer:

    A property that calculates its value dynamically when accessed -> Option C
  4. Quick Check:

    Computed property = dynamic value calculation [OK]
Hint: Computed properties calculate values on demand, not stored [OK]
Common Mistakes:
  • Confusing computed properties with stored properties
  • Thinking computed properties store values permanently
  • Assuming computed properties cannot have setters
2. Which of the following is the correct syntax for a computed property with a getter in Swift?
easy
A. var area: Double get { width * height }
B. var area: Double = width * height
C. var area: Double { get { width * height } }
D. var area: Double { return width * height }

Solution

  1. Step 1: Recall computed property syntax

    Computed properties use curly braces with a getter that returns a value.
  2. Step 2: Check each option

    var area: Double { return width * height } correctly uses braces and return keyword. var area: Double = width * height is stored property syntax. var area: Double { width * height } misses return keyword. var area: Double get { width * height } has incorrect getter syntax.
  3. Final Answer:

    var area: Double { return width * height } -> Option D
  4. Quick Check:

    Computed property getter uses braces and return [OK]
Hint: Use braces and return keyword for computed property getter [OK]
Common Mistakes:
  • Omitting the return keyword inside getter
  • Using equals sign instead of braces
  • Incorrect getter syntax without braces
3. Consider this Swift struct:
struct Rectangle {
  var width: Double
  var height: Double
  var area: Double {
    return width * height
  }
}
let rect = Rectangle(width: 3, height: 4)
print(rect.area)
What is the output?
medium
A. Compilation error
B. 12.0
C. 0.0
D. 7.0

Solution

  1. Step 1: Understand the struct and computed property

    Rectangle has width=3 and height=4. The computed property area returns width * height.
  2. Step 2: Calculate the area value

    3 * 4 = 12.0, so rect.area returns 12.0.
  3. Final Answer:

    12.0 -> Option B
  4. Quick Check:

    3 * 4 = 12.0 [OK]
Hint: Multiply width and height to get area from computed property [OK]
Common Mistakes:
  • Confusing stored and computed properties
  • Expecting area to be zero by default
  • Thinking code causes compilation error
4. Identify the error in this Swift code using a computed property:
struct Circle {
  var radius: Double
  var diameter: Double {
    get {
      return radius * 2
    }
    set {
      radius = newValue / 2
    }
  }
}
let c = Circle(radius: 5)
c.diameter = 20
print(c.radius)
medium
A. Cannot assign to property 'radius' because 'c' is a constant
B. Missing return keyword in getter
C. Setter syntax is incorrect
D. No error, code runs and prints 10

Solution

  1. Step 1: Analyze variable mutability

    Variable 'c' is declared with 'let', making it immutable (constant).
  2. Step 2: Understand setter behavior

    Setter tries to modify 'radius' property, but 'c' is constant, so assignment is not allowed.
  3. Final Answer:

    Cannot assign to property 'radius' because 'c' is a constant -> Option A
  4. Quick Check:

    Constant instance cannot change properties via setter [OK]
Hint: Use var, not let, to allow property setters to modify instance [OK]
Common Mistakes:
  • Ignoring that 'let' makes instance immutable
  • Thinking setter syntax is wrong
  • Missing return keyword confusion
5. You want to create a Swift struct Temperature that stores a temperature in Celsius and has a computed property fahrenheit to get and set the temperature in Fahrenheit. Which implementation correctly updates Celsius when Fahrenheit is set?
hard
A. struct Temperature { var celsius: Double var fahrenheit: Double { get { return celsius * 9 / 5 + 32 } set { celsius = (newValue - 32) * 5 / 9 } } }
B. struct Temperature { var celsius: Double var fahrenheit: Double { get { return celsius + 32 } set { celsius = newValue - 32 } } }
C. struct Temperature { var celsius: Double var fahrenheit: Double { get { return celsius * 2 + 30 } set { celsius = (newValue - 30) / 2 } } }
D. struct Temperature { var celsius: Double var fahrenheit: Double { get { return celsius * 5 / 9 + 32 } set { celsius = (newValue - 32) * 9 / 5 } } }

Solution

  1. Step 1: Recall temperature conversion formulas

    Fahrenheit = Celsius * 9/5 + 32; Celsius = (Fahrenheit - 32) * 5/9.
  2. Step 2: Check each option's formulas

    struct Temperature { var celsius: Double var fahrenheit: Double { get { return celsius * 9 / 5 + 32 } set { celsius = (newValue - 32) * 5 / 9 } } } uses correct formulas in getter and setter. The other options use incorrect formulas.
  3. Final Answer:

    The implementation with correct conversion formulas -> Option A
  4. Quick Check:

    Correct formulas = struct Temperature { var celsius: Double var fahrenheit: Double { get { return celsius * 9 / 5 + 32 } set { celsius = (newValue - 32) * 5 / 9 } } } [OK]
Hint: Use correct math formulas for temperature conversion in getter/setter [OK]
Common Mistakes:
  • Mixing up Celsius and Fahrenheit formulas
  • Using addition instead of multiplication
  • Forgetting to update Celsius in setter
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard