Properties must be assigned with self.model = model and self.year = 2020 to set the instance variables.
Final Answer:
Properties are not initialized using self keyword -> Option C
Quick Check:
Use self.property = value to initialize [OK]
Hint: Use self. to assign properties inside init [OK]
Common Mistakes:
Forgetting self. causes no property assignment
Adding return type to init
Thinking inheritance affects property assignment
5. You want to create a Swift class Book with properties title (String) and pages (Int). The designated initializer should allow setting both properties, but if pages is not provided, it defaults to 100. Which is the best way to write this initializer?
hard
A. init(title: String, pages: Int = 100) { self.title = title; self.pages = pages }
Step 1: Understand default parameter values in Swift
Swift allows default values in parameters, so pages: Int = 100 means pages defaults to 100 if not passed.
Step 2: Check initializer completeness and safety
The initializer init(title: String, pages: Int = 100) { self.title = title; self.pages = pages } sets both properties properly and safely with a default value. The version with only a title parameter cannot set pages explicitly. The version taking both parameters but only assigning title misses the pages assignment. The version using an optional Int? for pages with force unwrap is unsafe.
Final Answer:
init(title: String, pages: Int = 100) { self.title = title; self.pages = pages } -> Option A
Quick Check:
Default parameter = safe default value [OK]
Hint: Use default parameter values for optional init args [OK]
Common Mistakes:
Forgetting to assign all properties
Using force unwrap (!) unsafely
Not providing default value in parameter
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.