Bird
Raised Fist0
Swiftprogramming~20 mins

Extension syntax in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift Extension Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of extension method call
What is the output of this Swift code using an extension?
Swift
extension Int {
    func squared() -> Int {
        return self * self
    }
}

let number = 4
print(number.squared())
ACompilation error
B8
C16
D4
Attempts:
2 left
💡 Hint
Think about what multiplying a number by itself means.
❓ Predict Output
intermediate
2:00remaining
Extension adding computed property
What will be printed by this Swift code that uses an extension with a computed property?
Swift
extension String {
    var isLong: Bool {
        return self.count > 5
    }
}

let word = "Swift"
print(word.isLong)
Afalse
Btrue
CCompilation error
Dnil
Attempts:
2 left
💡 Hint
Count the letters in the word "Swift".
🔧 Debug
advanced
2:00remaining
Why does this extension cause a compile error?
This Swift extension code causes a compile error. What is the reason?
Swift
extension Int {
    func multiply(by: Int) -> Int {
        return self * by
    }

    func multiply(by: Int) -> Int {
        return self * by * 2
    }
}
AMissing return statement in one method
BSyntax error due to missing braces
CExtension cannot add methods to Int
DCannot have two methods with the same name and parameters in one extension
Attempts:
2 left
💡 Hint
Think about method overloading rules in Swift.
📝 Syntax
advanced
2:00remaining
Identify the correct extension syntax
Which option shows the correct syntax to add a method named 'greet' to the String type using an extension?
A
extension String {
    func greet() {
        print("Hello, \(self)!")
    }
}
B
extension String {
    greet() {
        print("Hello")
    }
}
C
extension String {
    func greet {
        print("Hi")
    }
}
D
extension String {
    func greet() -> String {
        print("Hello")
    }
}
Attempts:
2 left
💡 Hint
Remember the full function declaration syntax in Swift.
🚀 Application
expert
3:00remaining
Using extension to add a protocol conformance
Given this protocol and struct, which extension correctly adds conformance to the protocol by implementing the required method?
Swift
protocol Describable {
    func describe() -> String
}

struct Car {
    let brand: String
    let year: Int
}
A
extension Car {
    func describe() -> String {
        return "Car brand: \(brand), year: \(year)"
    }
}
B
extension Car: Describable {
    func describe() -> String {
        return "Car brand: \(brand), year: \(year)"
    }
}
C
extension Describable {
    func describe() -> String {
        return "Describable object"
    }
}
D
extension Car: Describable {
    func describe() {
        print("Car brand: \(brand), year: \(year)")
    }
}
Attempts:
2 left
💡 Hint
To conform to a protocol, implement all required methods with correct signatures in an extension that declares conformance.

Practice

(1/5)
1. What is the main purpose of using extension in Swift?
easy
A. To add new functionality to an existing type without modifying its original code
B. To create a new subclass from an existing class
C. To replace the original implementation of a type
D. To delete methods from a type

Solution

  1. Step 1: Understand what extensions do

    Extensions allow adding new features to existing types without changing their original source code.
  2. Step 2: Compare options with this definition

    Only adding new functionality to an existing type without modifying its original code correctly describes this purpose. Replacing original implementation refers to overriding, creating subclasses is inheritance, and deleting methods is unrelated to extensions.
  3. Final Answer:

    To add new functionality to an existing type without modifying its original code -> Option A
  4. Quick Check:

    Extension purpose = Add features without changing original code [OK]
Hint: Extensions add features without changing original code [OK]
Common Mistakes:
  • Confusing extensions with subclassing
  • Thinking extensions replace original code
  • Believing extensions can remove methods
2. Which of the following is the correct syntax to start an extension for the String type in Swift?
easy
A. extend String { }
B. extension String { }
C. extension String() { }
D. extension String[] { }

Solution

  1. Step 1: Recall Swift extension syntax

    The correct syntax starts with the keyword extension followed by the type name without parentheses or brackets.
  2. Step 2: Check each option

    extension String { } uses correct syntax. extend String { } uses invalid keyword. extension String() { } adds invalid parentheses. extension String[] { } uses invalid array notation.
  3. Final Answer:

    extension String { } -> Option B
  4. Quick Check:

    Extension syntax = extension TypeName { } [OK]
Hint: Use 'extension TypeName { }' without parentheses or brackets [OK]
Common Mistakes:
  • Using 'extend' instead of 'extension'
  • Adding parentheses after type name
  • Using brackets after type name
3. What will be the output of this Swift code?
extension Int {
    func squared() -> Int {
        return self * self
    }
}

let number = 4
print(number.squared())
medium
A. 16
B. 8
C. 4
D. Compile error

Solution

  1. Step 1: Understand the extension method

    The extension adds a method squared() to Int that returns the number multiplied by itself.
  2. Step 2: Calculate the output for number = 4

    Calling 4.squared() returns 4 * 4 = 16.
  3. Final Answer:

    16 -> Option A
  4. Quick Check:

    4 squared = 16 [OK]
Hint: Multiply the number by itself in the extension method [OK]
Common Mistakes:
  • Confusing squared with doubling
  • Expecting syntax error due to extension
  • Forgetting to return the value
4. Identify the error in this Swift extension code:
extension Double {
    func half() -> Double {
        self / 2
    }
}
medium
A. Missing return keyword in the method
B. Extension cannot add methods to Double
C. Method name 'half' is invalid
D. No error, code is correct

Solution

  1. Step 1: Check method syntax in extension

    In Swift, single-expression functions can omit the return keyword.
  2. Step 2: Verify if extension on Double is allowed

    Extensions can add methods to Double. The method name 'half' is valid.
  3. Final Answer:

    No error, code is correct -> Option D
  4. Quick Check:

    Single-expression methods can omit return [OK]
Hint: Single-expression methods can omit 'return' keyword [OK]
Common Mistakes:
  • Assuming 'return' is always required
  • Thinking extensions can't add methods to built-in types
  • Believing method names have restrictions
5. You want to add a computed property isEven to Int using an extension that returns true if the number is even, otherwise false. Which code correctly implements this?
hard
A. extension Int { var isEven: Bool { if self % 2 == 0 { return true } } }
B. extension Int { func isEven() -> Bool { return self % 2 == 0 } }
C. extension Int { var isEven: Bool { self % 2 == 0 } }
D. extension Int { var isEven: Bool { self / 2 == 0 } }

Solution

  1. Step 1: Understand computed property syntax

    Computed properties use var with a getter. extension Int { var isEven: Bool { self % 2 == 0 } } uses correct concise syntax; the func version declares a method, the if version lacks an else branch causing compile error, the division version has wrong logic.
  2. Step 2: Check logic correctness

    extension Int { var isEven: Bool { self % 2 == 0 } } uses concise expression self % 2 == 0 which returns true if even. extension Int { var isEven: Bool { if self % 2 == 0 { return true } } } omits else branch, causing compiler error. extension Int { var isEven: Bool { self / 2 == 0 } } incorrectly uses division instead of modulo.
  3. Step 3: Verify distractors

    The method requires parentheses to call like num.isEven(). Incomplete if fails to compile. Division fails for even numbers like 2 (2/2 == 1 != 0).
  4. Final Answer:

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

    Computed property with modulo check = extension Int { var isEven: Bool { self % 2 == 0 } } [OK]
Hint: Use computed property with 'self % 2 == 0' for even check [OK]
Common Mistakes:
  • Using division instead of modulo
  • Confusing method with computed property
  • Forgetting to return a Bool value
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