Bird
Raised Fist0
Swiftprogramming~20 mins

Convenience initializers in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift Convenience Initializer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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)
A4
B0
CCompilation error
Dnil
Attempts:
2 left
💡 Hint
Remember that the convenience initializer calls the designated initializer with 4 wheels.
❓ Predict Output
intermediate
2: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)
A""
B"Anonymous"
Cnil
DCompilation error
Attempts:
2 left
💡 Hint
The convenience initializer sets the name to "Anonymous".
❓ Predict Output
advanced
2: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)
ABuddy is a Dog
BBuddy is a Unknown
CRuntime error
DCompilation error
Attempts:
2 left
💡 Hint
The Dog convenience initializer calls init(name: "Buddy") which sets species to "Dog".
❓ Predict Output
advanced
2: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
    }
}
AError: Convenience initializer cannot have parameters
BNo error, compiles fine
CError: Missing return type
DError: 'self' used before calling designated initializer
Attempts:
2 left
💡 Hint
In convenience initializers, you must call a designated initializer before setting properties.
🧠 Conceptual
expert
3: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?
A1, 3, 2, 4
B2, 1, 3, 4
C1, 2, 3, 4
D3, 2, 1, 4
Attempts:
2 left
💡 Hint
Convenience initializers must call a designated initializer before properties are set.

Practice

(1/5)
1. What is the main purpose of a convenience initializer in Swift?
easy
A. To provide an easier way to create an object by calling a designated initializer
B. To override a superclass initializer
C. To create a completely new object without calling any other initializer
D. To deallocate an object before it is destroyed

Solution

  1. Step 1: Understand the role of convenience initializers

    Convenience initializers are shortcuts to create objects more easily by calling a designated initializer.
  2. Step 2: Compare options with this role

    Only To provide an easier way to create an object by calling a designated initializer describes this purpose correctly; others describe unrelated concepts.
  3. Final Answer:

    To provide an easier way to create an object by calling a designated initializer -> Option A
  4. Quick Check:

    Convenience initializer = shortcut calling designated initializer [OK]
Hint: Convenience initializers always call a designated initializer [OK]
Common Mistakes:
  • Thinking convenience initializers create objects independently
  • Confusing convenience initializers with deinitializers
  • Believing convenience initializers override superclass initializers
2. Which of the following is the correct syntax for a convenience initializer in Swift?
easy
A. convenience func init(name: String) { self.init() }
B. init convenience(name: String) { self.init() }
C. func convenience init(name: String) { self.init() }
D. convenience init(name: String) { self.init() }

Solution

  1. Step 1: Recall the correct syntax for convenience initializers

    In Swift, the keyword convenience comes before init without any function keyword.
  2. Step 2: Check each option

    convenience init(name: String) { self.init() } matches the correct syntax. Options B, C, and D misuse keywords or order.
  3. Final Answer:

    convenience init(name: String) { self.init() } -> Option D
  4. Quick Check:

    Keyword order: convenience before init [OK]
Hint: Remember: 'convenience' keyword comes before 'init' [OK]
Common Mistakes:
  • Placing 'convenience' after 'init'
  • Using 'func' keyword with initializers
  • Incorrect keyword order causing syntax errors
3. Consider the following Swift code:
class Car {
    var color: String
    init(color: String) {
        self.color = color
    }
    convenience init() {
        self.init(color: "Red")
    }
}
let myCar = Car()

What is the value of myCar.color?
medium
A. nil
B. "Red"
C. "" (empty string)
D. Compilation error

Solution

  1. Step 1: Understand the initializers in the Car class

    The designated initializer sets the color property. The convenience initializer calls the designated one with "Red".
  2. Step 2: Analyze the object creation

    Creating myCar with Car() calls the convenience initializer, which sets color to "Red".
  3. Final Answer:

    "Red" -> Option B
  4. Quick Check:

    Convenience init sets color to "Red" [OK]
Hint: Convenience init calls designated init with default values [OK]
Common Mistakes:
  • Assuming color is empty or nil without initialization
  • Thinking convenience init does not call designated init
  • Expecting compilation error due to missing default init
4. Identify the error in this Swift class using a convenience initializer:
class Person {
    var name: String
    convenience init() {
        self.name = "Unknown"
    }
    init(name: String) {
        self.name = name
    }
}
medium
A. Convenience initializer must call a designated initializer, not assign properties directly
B. Designated initializer must be marked as convenience
C. Property 'name' must be optional
D. No error, code is correct

Solution

  1. Step 1: Recall rules for convenience initializers

    A convenience initializer must call another initializer in the same class, not assign properties directly.
  2. Step 2: Check the convenience init implementation

    Here, self.name = "Unknown" is assigned directly, which is not allowed.
  3. Final Answer:

    Convenience initializer must call a designated initializer, not assign properties directly -> Option A
  4. Quick Check:

    Convenience init must call designated init [OK]
Hint: Convenience init cannot set properties directly; must call designated init [OK]
Common Mistakes:
  • Assigning properties directly in convenience initializer
  • Marking designated initializer as convenience
  • Making properties optional unnecessarily
5. You want to create a Swift class Rectangle with a designated initializer that takes width and height, and a convenience initializer that creates a square by taking only one side length. Which of the following implementations correctly uses a convenience initializer?
hard
A. class Rectangle { var width: Double var height: Double init(width: Double, height: Double) { self.width = width self.height = height } convenience init(side: Double) { self.width = side self.height = side } }
B. class Rectangle { var width: Double var height: Double convenience init(width: Double, height: Double) { self.width = width self.height = height } init(side: Double) { self.width = side self.height = side } }
C. class Rectangle { var width: Double var height: Double init(width: Double, height: Double) { self.width = width self.height = height } convenience init(side: Double) { self.init(width: side, height: side) } }
D. class Rectangle { var width: Double var height: Double init(width: Double, height: Double) { self.width = width self.height = height } convenience init(side: Double) { self.height = side } }

Solution

  1. Step 1: Identify designated and convenience initializers

    The designated initializer takes width and height. The convenience initializer should call the designated one.
  2. Step 2: Check each option for correct usage

    class Rectangle { var width: Double var height: Double init(width: Double, height: Double) { self.width = width self.height = height } convenience init(side: Double) { self.init(width: side, height: side) } } correctly defines the designated init and the convenience init calls it with equal sides. Others either misuse convenience keyword or assign properties directly.
  3. Final Answer:

    class Rectangle { var width: Double var height: Double init(width: Double, height: Double) { self.width = width self.height = height } convenience init(side: Double) { self.init(width: side, height: side) } } -> Option C
  4. Quick Check:

    Convenience init calls designated init with side values [OK]
Hint: Convenience init must call designated init with all required parameters [OK]
Common Mistakes:
  • Marking designated initializer as convenience
  • Assigning properties directly in convenience initializer
  • Not calling designated initializer from convenience initializer
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard