0
0
Swiftprogramming~20 mins

Adding 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 with a computed property?
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)
A7.0
B0.0
C12.0
DError: Cannot access computed property
Attempts:
2 left
💡 Hint
Remember that computed properties calculate their value when accessed.
Predict Output
intermediate
2:00remaining
What is the value of the computed property after changing stored properties?
Given this Swift class with a computed property, what will be printed after modifying the stored properties?
Swift
class Circle {
    var radius: Double
    var diameter: Double {
        return radius * 2
    }
    init(radius: Double) {
        self.radius = radius
    }
}

let circle = Circle(radius: 5)
print(circle.diameter)
circle.radius = 7
print(circle.diameter)
A10.0 and 14.0
B10.0 and 10.0
C5.0 and 7.0
DError: Cannot modify radius
Attempts:
2 left
💡 Hint
Computed properties reflect the current state of stored properties.
🔧 Debug
advanced
2:00remaining
Why does this computed property cause a compile-time error?
Examine the Swift struct below. Why does accessing the computed property cause a compile-time error?
Swift
struct Counter {
    var count: Int = 0
    mutating var nextCount: Int {
        count += 1
        return count
    }
}

var counter = Counter()
print(counter.nextCount)
AComputed properties cannot return Int values
BCannot modify 'count' inside a computed property without 'mutating' keyword
CMissing return statement in computed property
DStructs cannot have computed properties
Attempts:
2 left
💡 Hint
Think about mutability rules in Swift structs.
📝 Syntax
advanced
2:00remaining
Which option correctly defines a read-write computed property in Swift?
Choose the correct syntax for a computed property that can be read and written to.
A
var temperature: Double {
    get {
        return celsius
    }
}
B
var temperature: Double = {
    return celsius
}
C
var temperature: Double {
    return celsius
    set {
        celsius = newValue
    }
}
D
var temperature: Double {
    get {
        return celsius
    }
    set {
        celsius = newValue
    }
}
Attempts:
2 left
💡 Hint
A read-write computed property needs both get and set blocks properly defined.
🚀 Application
expert
2:00remaining
How many times is the computed property evaluated in this Swift code?
Consider this Swift code. How many times will the computed property 'description' be evaluated during execution?
Swift
struct Person {
    var firstName: String
    var lastName: String
    var description: String {
        print("Computing description")
        return "\(firstName) \(lastName)"
    }
}

let person = Person(firstName: "Jane", lastName: "Doe")
print(person.description)
print(person.description)
A2 times
B1 time
C0 times
D3 times
Attempts:
2 left
💡 Hint
Each access to a computed property runs its code.