0
0
Swiftprogramming~20 mins

Why extensions add capability without modifying in Swift - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Extensions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of extension adding method to struct
What is the output of this Swift code that uses an extension to add a method to a struct?
Swift
struct Rectangle {
    var width: Double
    var height: Double
}

extension Rectangle {
    func area() -> Double {
        return width * height
    }
}

let rect = Rectangle(width: 3, height: 4)
print(rect.area())
A7.0
B12.0
CError: Method 'area' not found
D0.0
Attempts:
2 left
💡 Hint
Extensions add methods without changing original struct code.
🧠 Conceptual
intermediate
1:30remaining
Why extensions do not modify original type source
Why do Swift extensions add new capabilities without modifying the original type's source code?
ABecause extensions replace the original type with a new type that includes added members
BBecause extensions are compiled separately and only add new members at runtime
CBecause extensions add new members at compile time without changing the original source code file
DBecause extensions copy the original type and modify the copy instead
Attempts:
2 left
💡 Hint
Think about how extensions are part of the same type but declared outside original code.
🔧 Debug
advanced
2:00remaining
Why does this extension method cause a compile error?
Given this code, why does the extension method cause a compile error?
Swift
struct Circle {
    var radius: Double
}

extension Circle {
    mutating func doubleRadius() {
        radius *= 2
    }
}

let c = Circle(radius: 5)
c.doubleRadius()
ABecause 'c' is a constant and cannot call a mutating method
BBecause extensions cannot add mutating methods
CBecause 'doubleRadius' must return a value
DBecause 'radius' is immutable inside the extension
Attempts:
2 left
💡 Hint
Check how 'let' and 'var' affect mutating methods.
📝 Syntax
advanced
1:30remaining
Which extension syntax correctly adds a computed property?
Which option correctly adds a computed property 'isEven' to Int using an extension?
A
extension Int {
    func isEven() -> Bool {
        return self % 2 == 0
    }
}
B
extension Int {
    let isEven: Bool = self % 2 == 0
}
C
extension Int {
    var isEven() -> Bool {
        return self % 2 == 0
    }
}
D
extension Int {
    var isEven: Bool {
        return self % 2 == 0
    }
}
Attempts:
2 left
💡 Hint
Computed properties use 'var' without parentheses.
🚀 Application
expert
2:30remaining
How extensions enable protocol conformance without modifying original type
How can Swift extensions add protocol conformance to a type without modifying its original source code?
ABy declaring the protocol conformance and implementing required methods inside an extension
BBy subclassing the original type and adding protocol conformance in the subclass
CBy copying the original type and adding protocol conformance to the copy
DBy modifying the original source code to include protocol conformance
Attempts:
2 left
💡 Hint
Extensions can declare protocol adoption and implement methods.