Bird
0
0

What will be the output of the following Swift code?

medium📝 Predict Output Q13 of 15
Swift - Functions
What will be the output of the following Swift code?
func add(_ a: Int, _ b: Int) -> Int { a + b }
var mathOperation: (Int, Int) -> Int = add
print(mathOperation(3, 4))
A7
B34
CError: Cannot assign function
D0
Step-by-Step Solution
Solution:
  1. Step 1: Understand function assignment

    The function 'add' matches the type (Int, Int) -> Int, so it can be assigned to 'mathOperation'.
  2. Step 2: Evaluate the function call

    Calling mathOperation(3, 4) calls add(3, 4), which returns 3 + 4 = 7.
  3. Final Answer:

    7 -> Option A
  4. Quick Check:

    Function call result = 7 [OK]
Quick Trick: Assign matching function types to variables to call them [OK]
Common Mistakes:
  • Expecting string concatenation instead of addition
  • Thinking function assignment causes error
  • Confusing function call syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes