0
0
Swiftprogramming~20 mins

Extension syntax in Swift - Practice Problems & Coding Challenges

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