Bird
0
0

Identify the error in this Swift code snippet:

medium📝 Debug Q14 of 15
Swift - Functions
Identify the error in this Swift code snippet:
var multiply: (Int, Int) -> Int
multiply = { a, b in a * b }
print(multiply(2))
AMissing return statement in closure
BVariable multiply not initialized
CCalling multiply with wrong number of arguments
DIncorrect closure syntax
Step-by-Step Solution
Solution:
  1. Step 1: Check closure assignment

    The closure { a, b in a * b } correctly matches (Int, Int) -> Int type.
  2. Step 2: Check function call

    Calling multiply(2) provides only one argument, but multiply expects two Ints.
  3. Final Answer:

    Calling multiply with wrong number of arguments -> Option C
  4. Quick Check:

    Argument count mismatch = Calling multiply with wrong number of arguments [OK]
Quick Trick: Match argument count with function type parameters [OK]
Common Mistakes:
  • Assuming closure needs explicit return
  • Ignoring argument count in function call
  • Thinking variable is uninitialized

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes