Bird
Raised Fist0
Swiftprogramming~20 mins

Memberwise initializer in Swift - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does a memberwise initializer in Swift do for a struct?
easy
A. It creates a function to print all properties.
B. It deletes all properties from the struct.
C. It automatically creates an initializer that sets all properties at once.
D. It converts the struct into a class.

Solution

  1. Step 1: Understand memberwise initializer purpose

    A memberwise initializer lets you create an instance by setting all properties in one call.
  2. Step 2: Recall Swift default behavior for structs

    Swift automatically provides this initializer for structs without custom initializers.
  3. Final Answer:

    It automatically creates an initializer that sets all properties at once. -> Option C
  4. Quick Check:

    Memberwise initializer = sets all properties at once [OK]
Hint: Memberwise initializer sets all struct properties at once [OK]
Common Mistakes:
  • Thinking it prints properties
  • Confusing with class initializers
  • Assuming it deletes properties
2. Which of the following is the correct way to use a memberwise initializer for this struct?
struct Book {
  var title: String
  var pages: Int
}
easy
A. let myBook = Book(title -> "Swift Guide", pages -> 300)
B. let myBook = Book("Swift Guide", 300)
C. let myBook = Book(title = "Swift Guide", pages = 300)
D. let myBook = Book(title: "Swift Guide", pages: 300)

Solution

  1. Step 1: Recall memberwise initializer syntax

    It uses parameter labels matching property names: title: and pages:.
  2. Step 2: Check each option

    let myBook = Book(title: "Swift Guide", pages: 300) uses correct syntax with labels and values. Others use wrong syntax or missing labels.
  3. Final Answer:

    let myBook = Book(title: "Swift Guide", pages: 300) -> Option D
  4. Quick Check:

    Memberwise initializer syntax = labels and values [OK]
Hint: Use property names as labels in initializer call [OK]
Common Mistakes:
  • Omitting labels in initializer
  • Using = instead of : in parameters
  • Using arrows -> instead of colons
3. What will be the output of this Swift code?
struct Point {
  var x: Int
  var y: Int
}

let p = Point(x: 5, y: 10)
print(p.x + p.y)
medium
A. 510
B. 15
C. x: 5, y: 10
D. Error: No memberwise initializer

Solution

  1. Step 1: Understand memberwise initializer usage

    The struct Point has two Int properties and uses the memberwise initializer to set x=5 and y=10.
  2. Step 2: Calculate the print output

    p.x + p.y = 5 + 10 = 15, so print outputs 15.
  3. Final Answer:

    15 -> Option B
  4. Quick Check:

    5 + 10 = 15 [OK]
Hint: Add property values after initializing with memberwise initializer [OK]
Common Mistakes:
  • Concatenating numbers as strings
  • Expecting a print of property names
  • Assuming no initializer exists
4. Identify the error in this code using a memberwise initializer:
struct Car {
  var brand: String
  var year: Int
}

let myCar = Car(brand: "Toyota")
medium
A. Missing value for 'year' property in initializer
B. Incorrect property name 'brand'
C. Cannot create instance of struct
D. Syntax error in struct declaration

Solution

  1. Step 1: Check memberwise initializer requirements

    All properties must be initialized. 'year' is missing in the initializer call.
  2. Step 2: Identify error cause

    Since 'year' is not given a value, the code will cause a compile error.
  3. Final Answer:

    Missing value for 'year' property in initializer -> Option A
  4. Quick Check:

    All properties must be set in memberwise initializer [OK]
Hint: Provide all properties when using memberwise initializer [OK]
Common Mistakes:
  • Forgetting to initialize all properties
  • Assuming default values without setting
  • Misnaming properties in initializer
5. Given this struct with a default property value:
struct User {
  var name: String
  var age: Int = 18
}

Which initializer call is valid and why?
hard
A. let u = User(name: "Anna") // age uses default value
B. let u = User(name: "Anna", age: 20) // must always provide age
C. let u = User() // no parameters allowed
D. let u = User(age: 18) // name can be omitted

Solution

  1. Step 1: Understand default property values in structs

    Properties with default values can be omitted in memberwise initializer calls.
  2. Step 2: Analyze each initializer call

    let u = User(name: "Anna") // age uses default value provides 'name' only; 'age' uses default 18, which is valid. Others omit required 'name' or incorrectly require all parameters.
  3. Final Answer:

    let u = User(name: "Anna") // age uses default value -> Option A
  4. Quick Check:

    Default property values allow skipping parameters [OK]
Hint: Skip properties with default values in initializer call [OK]
Common Mistakes:
  • Trying to omit non-default properties
  • Assuming all properties must be provided
  • Calling initializer with no parameters when required
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard