0
0
Swiftprogramming~10 mins

Convenience initializers in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a convenience initializer that sets a default name.

Swift
class Person {
    var name: String
    init(name: String) {
        self.name = name
    }
    convenience init() {
        self.init(name: [1])
    }
}
Drag options to blanks, or click blank then click option'
A"Unknown"
BUnknown
Cname
Dself.name
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use quotes around the string.
Trying to assign directly to self.name in the convenience initializer.
2fill in blank
medium

Complete the convenience initializer to set both name and age with default values.

Swift
class Person {
    var name: String
    var age: Int
    init(name: String, age: Int) {
        self.name = name
        self.age = age
    }
    convenience init() {
        self.init(name: [1], age: 0)
    }
}
Drag options to blanks, or click blank then click option'
Aname
BNoName
C"NoName"
Dself.name
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a variable name instead of a string literal.
Omitting quotes around the string.
3fill in blank
hard

Fix the error in the convenience initializer call to the designated initializer.

Swift
class Rectangle {
    var width: Double
    var height: Double
    init(width: Double, height: Double) {
        self.width = width
        self.height = height
    }
    convenience init(size: Double) {
        self.init(width: [1], height: size)
    }
}
Drag options to blanks, or click blank then click option'
Asize
Bself.size
Cwidth
Dheight
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'self.size' which is not defined.
Using 'width' which is not a parameter here.
4fill in blank
hard

Fill both blanks to complete the convenience initializer that calls the designated initializer with default values.

Swift
class Car {
    var make: String
    var year: Int
    init(make: String, year: Int) {
        self.make = make
        self.year = year
    }
    convenience init() {
        self.init(make: [1], year: [2])
    }
}
Drag options to blanks, or click blank then click option'
A"Toyota"
B2020
C2010
D"Honda"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string without quotes for make.
Using a year value that is not the default.
5fill in blank
hard

Fill all three blanks to create a convenience initializer that sets default values and calls the designated initializer.

Swift
class Book {
    var title: String
    var author: String
    var pages: Int
    init(title: String, author: String, pages: Int) {
        self.title = title
        self.author = author
        self.pages = pages
    }
    convenience init() {
        self.init(title: [1], author: [2], pages: [3])
    }
}
Drag options to blanks, or click blank then click option'
A"Untitled"
B"Unknown"
C100
D"Anonymous"
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up author and title values.
Using strings without quotes.