Bird
Raised Fist0
Swiftprogramming~20 mins

In-out parameters for mutation in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift In-Out 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 in-out parameter?

Consider this Swift function that doubles a number using an in-out parameter. What will be printed?

Swift
func doubleValue(_ number: inout Int) {
    number *= 2
}

var myNumber = 10
doubleValue(&myNumber)
print(myNumber)
A10
BCompilation error
C0
D20
Attempts:
2 left
💡 Hint

Remember that in-out parameters allow the function to modify the original variable.

❓ Predict Output
intermediate
2:00remaining
What error occurs when calling a function with in-out parameter incorrectly?

What error will this Swift code produce?

Swift
func increment(_ value: inout Int) {
    value += 1
}

let constantValue = 5
increment(&constantValue)
ACannot pass immutable value as inout argument
BRuntime error: nil value
CNo error, prints 6
DSyntax error: missing &
Attempts:
2 left
💡 Hint

In-out parameters require a variable that can be changed.

🔧 Debug
advanced
2:00remaining
Why does this Swift code cause a compile error with in-out parameters?

Identify the cause of the compile error in this Swift code:

Swift
func swapValues(_ a: inout Int, _ b: inout Int) {
    let temp = a
    a = b
    b = temp
}

var x = 3
swapValues(&x, &x)
ACannot pass the same variable twice as inout parameters
BMissing & before second argument
CFunction swapValues requires return type
DNo error, swaps values correctly
Attempts:
2 left
💡 Hint

Think about what happens if you try to modify the same variable twice at once.

❓ Predict Output
advanced
2:00remaining
What is the output of this nested in-out parameter mutation?

What will be printed by this Swift code?

Swift
func addOne(_ number: inout Int) {
    number += 1
}

func doubleAndAddOne(_ number: inout Int) {
    number *= 2
    addOne(&number)
}

var value = 4
doubleAndAddOne(&value)
print(value)
A10
B8
C9
DCompilation error
Attempts:
2 left
💡 Hint

Follow the changes step by step: first double, then add one.

🧠 Conceptual
expert
2:00remaining
What is the main reason Swift uses in-out parameters instead of returning mutated values?

Why does Swift use in-out parameters for mutation instead of just returning a new value?

ABecause Swift does not support returning values from functions
BTo allow functions to modify variables directly without copying large data structures
CTo prevent any changes to variables outside the function
DBecause in-out parameters are required for all functions in Swift
Attempts:
2 left
💡 Hint

Think about performance and memory when working with big data.

Practice

(1/5)
1.

What does the inout keyword do in Swift functions?

easy
A. Allows the function to modify the original variable passed in.
B. Prevents the function from changing the variable.
C. Creates a copy of the variable that cannot be changed.
D. Makes the variable constant inside the function.

Solution

  1. Step 1: Understand the role of inout

    The inout keyword allows a function to receive a variable and modify it directly.
  2. Step 2: Effect on the original variable

    Changes made inside the function affect the original variable outside the function.
  3. Final Answer:

    Allows the function to modify the original variable passed in. -> Option A
  4. Quick Check:

    inout means modify original variable [OK]
Hint: Remember: inout means function can change your variable [OK]
Common Mistakes:
  • Thinking inout creates a copy instead of modifying original
  • Forgetting to use & when calling the function
  • Assuming variables are constant inside the function
2.

Which of the following is the correct way to declare a function with an inout parameter in Swift?

func updateValue(____ value: inout Int) { value += 10 }
easy
A. func updateValue(inout value: Int)
B. func updateValue(value: inout Int)
C. func updateValue(value inout Int)
D. func updateValue(inout value in Int)

Solution

  1. Step 1: Recall Swift syntax for inout parameters

    The correct syntax places inout before the type, after the parameter name: paramName: inout Type.
  2. Step 2: Match the options

    func updateValue(value: inout Int) matches this syntax exactly: value: inout Int.
  3. Final Answer:

    func updateValue(value: inout Int) -> Option B
  4. Quick Check:

    Parameter syntax = name: inout Type [OK]
Hint: Remember: inout goes after parameter name, before type [OK]
Common Mistakes:
  • Placing inout before parameter name
  • Swapping parameter name and type order
  • Using invalid syntax like 'inout value in Int'
3.

What will be the output of this Swift code?

func doubleValue(_ number: inout Int) {
    number *= 2
}

var myNum = 5
doubleValue(&myNum)
print(myNum)
medium
A. 5
B. Error: missing & in function call
C. 10
D. Error: cannot modify constant

Solution

  1. Step 1: Understand the function behavior

    The function doubles the value of the passed variable by multiplying it by 2.
  2. Step 2: Trace the code execution

    Variable myNum starts at 5, is passed with & to allow mutation, so after the function call it becomes 10.
  3. Final Answer:

    10 -> Option C
  4. Quick Check:

    In-out parameter doubles 5 to 10 [OK]
Hint: Use & when calling to let function change variable [OK]
Common Mistakes:
  • Forgetting & when calling function
  • Expecting original value unchanged
  • Confusing parameter passing with copy
4.

Identify the error in this Swift code snippet:

func increment(_ value: inout Int) {
    value += 1
}

let number = 10
increment(&number)
medium
A. Cannot pass a constant as an inout parameter.
B. Missing & when calling the function.
C. Function parameter should not be inout.
D. Function must return the new value.

Solution

  1. Step 1: Check variable mutability

    The variable number is declared with let, making it a constant.
  2. Step 2: Understand inout parameter requirements

    Inout parameters require a variable that can be modified, so constants cannot be passed.
  3. Final Answer:

    Cannot pass a constant as an inout parameter. -> Option A
  4. Quick Check:

    Inout needs variable, not constant [OK]
Hint: Only variables (var), not constants (let), can be inout [OK]
Common Mistakes:
  • Trying to pass constants as inout parameters
  • Forgetting & when calling function
  • Thinking function must return value when using inout
5.

You want to write a function swapValues that swaps two integers using inout parameters. Which of the following implementations correctly swaps the values?

func swapValues(_ a: inout Int, _ b: inout Int) {
    ____
}
hard
A. let temp = a temp = b b = temp
B. a = b b = a
C. a = b b = a - b a = a - b
D. var temp = a a = b b = temp

Solution

  1. Step 1: Understand swapping logic

    To swap two values, you need a temporary variable to hold one value while assigning the other.
  2. Step 2: Check each option

    var temp = a a = b b = temp correctly declares a temporary variable with var and swaps values safely. let temp = a temp = b b = temp uses let which is immutable and causes error. a = b b = a overwrites a before assigning b. a = b b = a - b a = a - b uses arithmetic swap but is more complex and not recommended here.
  3. Final Answer:

    var temp = a a = b b = temp -> Option D
  4. Quick Check:

    Use temp variable with var to swap values [OK]
Hint: Use a temporary variable declared with var to swap values [OK]
Common Mistakes:
  • Using let for temporary variable causing immutability error
  • Overwriting one variable before saving its value
  • Trying to swap without a temporary variable incorrectly
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