Bird
Raised Fist0
Swiftprogramming~20 mins

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

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

Practice

(1/5)
1. Why do Swift extensions add capability without modifying the original type?
easy
A. Because they require subclassing the original type
B. Because they add new features without changing existing code
C. Because they delete old methods before adding new ones
D. Because they replace the original type completely

Solution

  1. Step 1: Understand what extensions do

    Extensions add new methods or properties to existing types without altering the original source code.
  2. Step 2: Recognize the benefit

    This allows adding features safely without changing or breaking existing code.
  3. Final Answer:

    Because they add new features without changing existing code -> Option B
  4. Quick Check:

    Extensions add features without modification = D [OK]
Hint: Extensions add, not change, original code [OK]
Common Mistakes:
  • Thinking extensions replace original types
  • Believing extensions delete old methods
  • Confusing extensions with subclassing
2. Which of the following is the correct syntax to add a method using an extension in Swift?
easy
A. extension MyType { func newMethod() { } }
B. extend MyType { func newMethod() { } }
C. extension MyType: { func newMethod() { } }
D. extension MyType func newMethod() { }

Solution

  1. Step 1: Recall Swift extension syntax

    Extensions start with the keyword 'extension' followed by the type name and braces enclosing new members.
  2. Step 2: Check each option

    extension MyType { func newMethod() { } } matches the correct syntax: 'extension MyType { func newMethod() { } }'. Others have syntax errors.
  3. Final Answer:

    extension MyType { func newMethod() { } } -> Option A
  4. Quick Check:

    Correct extension syntax = A [OK]
Hint: Remember: 'extension TypeName { ... }' [OK]
Common Mistakes:
  • Using 'extend' instead of 'extension'
  • Adding colon after type name
  • Omitting braces around methods
3. What will be the output of this Swift code?
struct Person { var name: String }

extension Person {
    func greet() -> String {
        return "Hello, \(name)!"
    }
}

let p = Person(name: "Anna")
print(p.greet())
medium
A. Hello, Anna!
B. Hello, !
C. Error: greet() not found
D. Hello, name!

Solution

  1. Step 1: Understand the extension adds greet() method

    The extension adds a greet() method to Person that returns "Hello, \(name)!" using the instance's name.
  2. Step 2: Check the instance and method call

    Creating Person(name: "Anna") and calling greet() returns "Hello, Anna!".
  3. Final Answer:

    Hello, Anna! -> Option A
  4. Quick Check:

    Extension method works, output = Hello, Anna! [OK]
Hint: Extension methods use instance data normally [OK]
Common Mistakes:
  • Assuming extension methods can't access properties
  • Expecting compile error for extension method
  • Confusing string interpolation syntax
4. Identify the error in this Swift extension code:
extension Int {
    func squared() -> Int {
        return self * self
    }
}

let result = 5.squared
print(result)
medium
A. Cannot extend Int type
B. squared() must be static method
C. self keyword not allowed in extensions
D. Missing parentheses when calling squared()

Solution

  1. Step 1: Check method call syntax

    The method squared() is defined correctly, but calling 5.squared misses parentheses.
  2. Step 2: Understand method call requirements

    In Swift, methods must be called with parentheses even if no parameters, so 5.squared() is correct.
  3. Final Answer:

    Missing parentheses when calling squared() -> Option D
  4. Quick Check:

    Method call needs () = B [OK]
Hint: Call methods with () even if no parameters [OK]
Common Mistakes:
  • Calling methods without parentheses
  • Thinking Int cannot be extended
  • Believing self is disallowed in extensions
5. You want to add a computed property isEven to Int using an extension that returns true if the number is even. Which code correctly achieves this without modifying the original Int type?
hard
A. extension Int { static var isEven: Bool { self % 2 == 0 } }
B. extension Int { func isEven() -> Bool { self % 2 == 0 } }
C. extension Int { var isEven: Bool { self % 2 == 0 } }
D. extension Int { var isEven: Bool { return self / 2 == 0 } }

Solution

  1. Step 1: Understand computed property syntax

    Computed properties use 'var name: Type { expression }' without parentheses.
  2. Step 2: Check each option

    extension Int { var isEven: Bool { self % 2 == 0 } } correctly defines a computed property isEven returning true if self % 2 == 0. extension Int { func isEven() -> Bool { self % 2 == 0 } } uses a method, not a property. extension Int { static var isEven: Bool { self % 2 == 0 } } incorrectly uses static. extension Int { var isEven: Bool { return self / 2 == 0 } } uses wrong logic (division instead of modulo).
  3. Final Answer:

    extension Int { var isEven: Bool { self % 2 == 0 } } -> Option C
  4. Quick Check:

    Computed property with modulo check = A [OK]
Hint: Computed properties use var without parentheses [OK]
Common Mistakes:
  • Using func instead of var for computed property
  • Making property static when it should be instance
  • Using division instead of modulo for even check
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard