0
0
Swiftprogramming~20 mins

Computed properties in Swift - Practice Problems & Coding Challenges

Choose your learning style9 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.