0
0
Swiftprogramming~20 mins

Enum methods and computed properties in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Enum Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of enum method with computed property
What is the output of this Swift code?
Swift
enum Direction {
    case north, south, east, west
    
    var isVertical: Bool {
        return self == .north || self == .south
    }
    
    func description() -> String {
        return isVertical ? "Vertical" : "Horizontal"
    }
}

let dir = Direction.east
print(dir.description())
A"Vertical"
B"north"
CCompilation error
D"Horizontal"
Attempts:
2 left
💡 Hint
Check how the computed property 'isVertical' is defined and used in the method.
Predict Output
intermediate
2:00remaining
Computed property with associated values
What will be printed by this Swift code?
Swift
enum Result {
    case success(Int)
    case failure(String)
    
    var message: String {
        switch self {
        case .success(let code):
            return "Success with code \(code)"
        case .failure(let error):
            return "Failed: \(error)"
        }
    }
}

let res = Result.failure("Network error")
print(res.message)
A"Failed: Network error"
B"Success with code 0"
CRuntime error
D"Failed: nil"
Attempts:
2 left
💡 Hint
Look at how the associated value is extracted in the computed property.
🔧 Debug
advanced
2:00remaining
Identify the error in enum method
What error does this Swift code produce when compiled?
Swift
enum Status {
    case ready, waiting
    
    func isReady() -> Bool {
        switch self {
        case .ready:
            return true
        case .waiting
            return false
        }
    }
}
ARuntime error: Missing return statement
BTypeError: Cannot return Bool from function
CSyntaxError: Missing colon after 'case .waiting'
DNo error, compiles fine
Attempts:
2 left
💡 Hint
Check the switch case syntax carefully.
Predict Output
advanced
2:00remaining
Computed property returning different types
What is the output of this Swift code?
Swift
enum Measurement {
    case weight(Double)
    case count(Int)
    
    var description: String {
        switch self {
        case .weight(let w):
            return "Weight: \(w) kg"
        case .count(let c):
            return "Count: \(c) items"
        }
    }
}

let m = Measurement.weight(3.5)
print(m.description)
ACompilation error: mismatched types
B"Weight: 3.5 kg"
C"Count: 3.5 items"
D"Weight: 3 kg"
Attempts:
2 left
💡 Hint
Check the associated value type and string interpolation.
🧠 Conceptual
expert
2:00remaining
Why use computed properties in enums?
Which of the following best explains why computed properties are useful in Swift enums?
AThey enable adding dynamic behavior or derived values based on the enum case and its associated values.
BThey allow storing additional data inside enum cases without associated values.
CThey make enums mutable so their cases can change at runtime.
DThey automatically generate raw values for enum cases.
Attempts:
2 left
💡 Hint
Think about what computed properties do in general.