Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use quotes around the string.
Trying to assign directly to self.name in the convenience initializer.
✗ Incorrect
The convenience initializer calls the designated initializer with a default string "Unknown" as the name.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a variable name instead of a string literal.
Omitting quotes around the string.
✗ Incorrect
The convenience initializer calls the designated initializer with a default name "NoName" and age 0.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'self.size' which is not defined.
Using 'width' which is not a parameter here.
✗ Incorrect
The convenience initializer uses the parameter 'size' for both width and height by passing it directly.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string without quotes for make.
Using a year value that is not the default.
✗ Incorrect
The convenience initializer calls the designated initializer with default make "Toyota" and year 2020.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up author and title values.
Using strings without quotes.
✗ Incorrect
The convenience initializer calls the designated initializer with default title "Untitled", author "Unknown", and pages 100.