0
0
Swiftprogramming~20 mins

Adding initializers via extension in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Initializer Extension Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of initializer added via extension
What is the output of this Swift code that adds an initializer via extension?
Swift
struct Point {
    var x: Int
    var y: Int
}

extension Point {
    init() {
        self.x = 0
        self.y = 0
    }
}

let p = Point()
print("(\(p.x), \(p.y))")
A(x: 0, y: 0)
B(nil, nil)
CCompilation error
D(0, 0)
Attempts:
2 left
💡 Hint
Think about what the extension initializer sets the properties to.
Predict Output
intermediate
2:00remaining
Value of property after extension initializer
What is the value of the property 'name' after creating an instance using the extension initializer?
Swift
class Person {
    var name: String
    init(name: String) {
        self.name = name
    }
}

extension Person {
    convenience init() {
        self.init(name: "Unknown")
    }
}

let p = Person()
print(p.name)
AEmpty string
Bnil
CUnknown
DCompilation error
Attempts:
2 left
💡 Hint
Check what the convenience initializer passes to the designated initializer.
🔧 Debug
advanced
2:00remaining
Why does this extension initializer cause a compile error?
Consider this Swift code. Why does the extension initializer cause a compile error?
Swift
struct Rectangle {
    var width: Int
    var height: Int
}

extension Rectangle {
    init(area: Int) {
        self.width = area
        // Missing height initialization
    }
}
AAll stored properties must be initialized before use, height is missing.
BExtensions cannot add initializers to structs.
CThe init method must be marked as convenience.
DThe code is valid and compiles without errors.
Attempts:
2 left
💡 Hint
Check if all stored properties are initialized in the initializer.
📝 Syntax
advanced
2:00remaining
Which extension initializer syntax is correct?
Which of the following extension initializers for struct Circle is syntactically correct?
Swift
struct Circle {
    var radius: Double
}

// Choose the correct extension initializer syntax:
A
extension Circle {
    init(diameter: Double) {
        self.radius = diameter / 2
    }
}
B
extension Circle {
    convenience init(diameter: Double) {
        self.radius = diameter / 2
    }
}
C
extension Circle {
    init(diameter: Double) {
        radius = diameter / 2
    }
}
D
extension Circle {
    convenience init(diameter: Double) {
        radius = diameter / 2
    }
}
Attempts:
2 left
💡 Hint
Remember that convenience initializers are only for classes, not structs.
🚀 Application
expert
2:00remaining
How many initializers does this class have after extension?
Given this Swift class and its extension, how many initializers does the class have in total?
Swift
class Vehicle {
    var wheels: Int
    init(wheels: Int) {
        self.wheels = wheels
    }
}

extension Vehicle {
    convenience init() {
        self.init(wheels: 4)
    }
}
A1
B2
C3
D0
Attempts:
2 left
💡 Hint
Count both the original and extension initializers.