0
0
Swiftprogramming~20 mins

Initializers and designated init in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Initializer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Swift initializer code?
Consider the following Swift class with a designated initializer and a convenience initializer. What will be printed when creating an instance with let obj = MyClass()?
Swift
class MyClass {
    var value: Int
    
    init(value: Int) {
        self.value = value
        print("Designated init with value: \(value)")
    }
    
    convenience init() {
        self.init(value: 10)
        print("Convenience init called")
    }
}

let obj = MyClass()
ANo output printed
BConvenience init called\nDesignated init with value: 10
CDesignated init with value: 0\nConvenience init called
DDesignated init with value: 10\nConvenience init called
Attempts:
2 left
💡 Hint
Remember that convenience initializers must call a designated initializer first.
Predict Output
intermediate
2:00remaining
What error does this Swift initializer code produce?
What error will this Swift class produce when compiled?
Swift
class Person {
    var name: String
    
    convenience init(name: String) {
        self.name = name
    }
}
AError: Property 'name' not initialized before use
BError: Convenience initializer must call a designated initializer
CNo error, compiles successfully
DError: Missing required initializer
Attempts:
2 left
💡 Hint
Check the rules for convenience initializers in Swift.
🔧 Debug
advanced
2:00remaining
Why does this Swift struct initializer code fail to compile?
This Swift struct has a custom initializer. Why does it fail to compile?
Swift
struct Rectangle {
    var width: Double
    var height: Double
    
    init(width: Double) {
        self.width = width
    }
}
AError: Property 'height' not initialized before use
BError: Structs cannot have custom initializers
CNo error, compiles successfully
DError: Initializer missing 'self' keyword
Attempts:
2 left
💡 Hint
All stored properties must be initialized before the initializer ends.
Predict Output
advanced
2:00remaining
What is the output of this Swift class inheritance initializer code?
Given the following Swift classes, what will be printed when creating let c = Child()?
Swift
class Parent {
    init() {
        print("Parent init")
    }
}

class Child: Parent {
    override init() {
        print("Child init start")
        super.init()
        print("Child init end")
    }
}

let c = Child()
AChild init start\nParent init\nChild init end
BParent init\nChild init start\nChild init end
CChild init start\nChild init end\nParent init
DParent init\nChild init end\nChild init start
Attempts:
2 left
💡 Hint
Remember the order of initializer calls in inheritance.
Predict Output
expert
3:00remaining
What is the value of 'description' after this Swift initializer code runs?
Analyze the following Swift class with a designated initializer and a computed property. What is the value of obj.description after let obj = Vehicle(type: "Car", wheels: 4)?
Swift
class Vehicle {
    var type: String
    var wheels: Int
    
    var description: String {
        "\(type) with \(wheels) wheels"
    }
    
    init(type: String, wheels: Int) {
        self.type = type
        self.wheels = wheels
    }
}

let obj = Vehicle(type: "Car", wheels: 4)
A"Vehicle with 4 wheels"
B"Car with wheels"
C"Car with 4 wheels"
D"type with wheels"
Attempts:
2 left
💡 Hint
Check how the computed property uses stored properties.