Challenge - 5 Problems
Swift Convenience Initializer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a class with convenience initializer
What will be printed when the following Swift code runs?
Swift
class Vehicle { var wheels: Int init(wheels: Int) { self.wheels = wheels } convenience init() { self.init(wheels: 4) } } let car = Vehicle() print(car.wheels)
Attempts:
2 left
💡 Hint
Remember that the convenience initializer calls the designated initializer with 4 wheels.
✗ Incorrect
The convenience initializer calls init(wheels: 4), so the wheels property is set to 4. Printing car.wheels outputs 4.
❓ Predict Output
intermediate2:00remaining
Value of property after using convenience initializer with parameters
What is the value of 'name' after running this Swift code?
Swift
class Person { var name: String init(name: String) { self.name = name } convenience init() { self.init(name: "Anonymous") } } let p = Person() print(p.name)
Attempts:
2 left
💡 Hint
The convenience initializer sets the name to "Anonymous".
✗ Incorrect
The convenience initializer calls init(name: "Anonymous"), so p.name is "Anonymous".
❓ Predict Output
advanced2:00remaining
Output of subclass using convenience initializer
What will be printed when this Swift code runs?
Swift
class Animal { var species: String init(species: String) { self.species = species } convenience init() { self.init(species: "Unknown") } } class Dog: Animal { var name: String init(name: String) { self.name = name super.init(species: "Dog") } convenience init() { self.init(name: "Buddy") } } let dog = Dog() print(dog.name + " is a " + dog.species)
Attempts:
2 left
💡 Hint
The Dog convenience initializer calls init(name: "Buddy") which sets species to "Dog".
✗ Incorrect
Dog's convenience init calls init(name: "Buddy"), which sets name and calls super.init(species: "Dog"), so output is "Buddy is a Dog".
❓ Predict Output
advanced2:00remaining
Error type from incorrect convenience initializer
What error does this Swift code produce?
Swift
class Box { var size: Int init(size: Int) { self.size = size } convenience init() { self.size = 10 } }
Attempts:
2 left
💡 Hint
In convenience initializers, you must call a designated initializer before setting properties.
✗ Incorrect
The convenience initializer tries to set self.size before calling a designated initializer, which is not allowed and causes a compile-time error.
🧠 Conceptual
expert3:00remaining
Correct order of initialization in Swift convenience initializer
Which sequence correctly describes the order of steps when a convenience initializer is called in Swift?
Attempts:
2 left
💡 Hint
Convenience initializers must call a designated initializer before properties are set.
✗ Incorrect
First, the convenience initializer is called (1), then it calls a designated initializer (2), which initializes all stored properties (3), and finally returns the fully initialized instance (4).