Bird
0
0

What will be the output of this Swift code?

medium📝 Predict Output Q13 of 15
Swift - Functions
What will be the output of this Swift code?
func multiplyByTwo(_ number: Int) -> Int {
    return number * 2
}

let operation = multiplyByTwo
print(operation(5))
A5
BmultiplyByTwo
C10
DError: Cannot assign function to variable
Step-by-Step Solution
Solution:
  1. Step 1: Understand function assignment and call

    The function multiplyByTwo is assigned to 'operation' without calling it, so 'operation' is a function reference.
  2. Step 2: Call the function through the variable

    Calling operation(5) runs multiplyByTwo(5), which returns 5 * 2 = 10.
  3. Final Answer:

    10 -> Option C
  4. Quick Check:

    Function call via variable returns 10 [OK]
Quick Trick: Calling assigned function runs original logic [OK]
Common Mistakes:
  • Expecting variable to hold result, not function
  • Confusing function name with string output
  • Thinking assignment causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes