Challenge - 5 Problems
Swift Computed Properties Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Remember that computed properties calculate their value when accessed.
✗ Incorrect
The computed property 'area' multiplies width and height. For width 3 and height 4, area is 12.0.
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Computed properties reflect the current state of stored properties.
✗ Incorrect
The diameter is always twice the radius. Initially 5*2=10, after radius changes to 7, diameter is 14.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Think about mutability rules in Swift structs.
✗ Incorrect
In Swift, modifying a struct's property inside a computed property requires the property to be marked 'mutating'.
📝 Syntax
advanced2: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.
Attempts:
2 left
💡 Hint
A read-write computed property needs both get and set blocks properly defined.
✗ Incorrect
Option D correctly uses get and set blocks. Option D is invalid syntax. Option D has set after return without get block. Option D is read-only.
🚀 Application
expert2: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)
Attempts:
2 left
💡 Hint
Each access to a computed property runs its code.
✗ Incorrect
The computed property runs its code every time it is accessed. Here, it is accessed twice, so printed twice.