Challenge - 5 Problems
Swift Initializer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()
Attempts:
2 left
💡 Hint
Remember that convenience initializers must call a designated initializer first.
✗ Incorrect
The convenience initializer calls the designated initializer first, which prints the first line. Then the convenience initializer prints its own message.
❓ Predict Output
intermediate2: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 } }
Attempts:
2 left
💡 Hint
Check the rules for convenience initializers in Swift.
✗ Incorrect
Convenience initializers must call a designated initializer. Here, the convenience init tries to assign a property directly without calling a designated init, causing an error.
🔧 Debug
advanced2: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 } }
Attempts:
2 left
💡 Hint
All stored properties must be initialized before the initializer ends.
✗ Incorrect
The property 'height' is not given a value in the initializer, so the compiler raises an error.
❓ Predict Output
advanced2: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()
Attempts:
2 left
💡 Hint
Remember the order of initializer calls in inheritance.
✗ Incorrect
The child initializer runs its code, then calls super.init(), then continues after that call.
❓ Predict Output
expert3: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)
Attempts:
2 left
💡 Hint
Check how the computed property uses stored properties.
✗ Incorrect
The computed property returns a string combining the 'type' and 'wheels' values initialized in the constructor.