0
0
Swiftprogramming~10 mins

Memberwise initializer 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 create a struct with a memberwise initializer.

Swift
struct Person {
    var name: String
    var age: Int
}

let person = Person([1]: "Alice", age: 30)
Drag options to blanks, or click blank then click option'
Aage
BPerson
Cname
Dinit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'age' as the first label instead of 'name'.
Trying to call 'init' explicitly.
2fill in blank
medium

Complete the code to create an instance of the struct using the memberwise initializer.

Swift
struct Car {
    var make: String
    var year: Int
}

let car = Car(make: "Toyota", [1]: 2020)
Drag options to blanks, or click blank then click option'
Ayear
Binit
Cmake
Dmodel
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'model' instead of 'year'.
Repeating 'make' as the label.
3fill in blank
hard

Fix the error in the code by completing the memberwise initializer call.

Swift
struct Book {
    var title: String
    var pages: Int
}

let book = Book(title: "Swift Guide", [1]: 250)
Drag options to blanks, or click blank then click option'
Apages
Bcount
Clength
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'length' or 'size' instead of 'pages'.
Omitting the label for the second argument.
4fill in blank
hard

Fill both blanks to create a struct instance using the memberwise initializer.

Swift
struct Rectangle {
    var width: Double
    var height: Double
}

let rect = Rectangle([1]: 10.5, [2]: 20.0)
Drag options to blanks, or click blank then click option'
Awidth
Blength
Cheight
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'length' or 'size' instead of 'width' or 'height'.
Swapping the labels.
5fill in blank
hard

Fill all three blanks to create a struct instance with the memberwise initializer.

Swift
struct Student {
    var name: String
    var grade: Int
    var passed: Bool
}

let student = Student([1]: "John", [2]: 9, [3]: true)
Drag options to blanks, or click blank then click option'
Aname
Bgrade
Cpassed
Dscore
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'score' instead of 'grade' or 'passed'.
Mixing up the labels.