0
0
Swiftprogramming~20 mins

Memberwise initializer in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Memberwise Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Swift code using a memberwise initializer?

Consider the following Swift struct and code:

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

let myBook = Book(title: "Swift Guide", pages: 300)
print(myBook.pages)

What will be printed?

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

let myBook = Book(title: "Swift Guide", pages: 300)
print(myBook.pages)
A300
BSwift Guide
C0
DCompilation error
Attempts:
2 left
💡 Hint

Memberwise initializer sets properties in the order you provide them.

Predict Output
intermediate
2:00remaining
What error does this Swift code produce?

Look at this Swift struct and code:

struct Car {
    var brand: String
    var year: Int
}

let myCar = Car(brand: "Toyota")

What error will this code cause?

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

let myCar = Car(brand: "Toyota")
ANo error, compiles fine
BType mismatch error
CMissing argument for parameter 'year' in call
DCannot assign value of type 'String' to type 'Int'
Attempts:
2 left
💡 Hint

Memberwise initializer requires all properties to be initialized unless default values are provided.

🔧 Debug
advanced
2:00remaining
Why does this Swift struct fail to compile?

Examine this Swift code:

struct Person {
    var name: String
    var age: Int

    init(name: String) {
        self.name = name
    }
}

let p = Person(name: "Anna", age: 25)

Why does this code fail to compile?

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

    init(name: String) {
        self.name = name
    }
}

let p = Person(name: "Anna", age: 25)
ACustom init disables memberwise initializer, so 'age' parameter is missing
BCannot assign 'age' after init is defined
CStructs cannot have custom initializers
DNo error, compiles fine
Attempts:
2 left
💡 Hint

Adding a custom initializer removes the automatic memberwise initializer.

📝 Syntax
advanced
2:00remaining
Which option correctly uses the memberwise initializer for this Swift struct?

Given this struct:

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

Which code correctly creates a Rectangle instance using the memberwise initializer?

Swift
struct Rectangle {
    var width: Double
    var height: Double
}
Alet rect = Rectangle(width = 5.0, height = 10.0)
Blet rect = Rectangle(5.0, 10.0)
Clet rect = Rectangle(width: 5, height: "10")
Dlet rect = Rectangle(width: 5.0, height: 10.0)
Attempts:
2 left
💡 Hint

Memberwise initializer requires parameter labels matching property names.

🚀 Application
expert
2:00remaining
How many properties does this Swift struct instance have after initialization?

Consider this Swift struct with default values:

struct Laptop {
    var brand: String
    var ramGB: Int = 8
    var storageGB: Int
}

let myLaptop = Laptop(brand: "Apple", storageGB: 256)

How many properties does myLaptop have initialized after this code runs?

Swift
struct Laptop {
    var brand: String
    var ramGB: Int = 8
    var storageGB: Int
}

let myLaptop = Laptop(brand: "Apple", storageGB: 256)
A2
B3
C1
DCompilation error
Attempts:
2 left
💡 Hint

Default values count as initialized properties.