Bird
0
0

What will be the output of this Swift code?

medium📝 Predict Output Q4 of 15
Swift - Functions

What will be the output of this Swift code?

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

var count = 5
increment(&count)
print(count)
A5
B6
CError: Cannot pass variable as inout
DError: Missing & when calling function
Step-by-Step Solution
Solution:
  1. Step 1: Understand the function behavior

    The function increments the value of the variable passed by 1 using an inout parameter.
  2. Step 2: Trace the code execution

    Variable count starts at 5, then increment(&count) increases it to 6, so print(count) outputs 6.
  3. Final Answer:

    6 -> Option B
  4. Quick Check:

    inout lets function modify original variable [OK]
Quick Trick: Inout parameters let functions change original variables [OK]
Common Mistakes:
  • Forgetting to use & when calling the function
  • Expecting the original variable to remain unchanged
  • Confusing inout with returning a new value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes