Bird
0
0

What will be the output of this Swift code?

medium📝 Predict Output Q5 of 15
Swift - Functions
What will be the output of this Swift code?
func multiply(_ numbers: Int...) -> Int {
    var result = 1
    for num in numbers {
        result *= num
    }
    return result
}
print(multiply(2, 3, 4))
AError: Cannot multiply variadic parameters
B24
C9
D234
Step-by-Step Solution
Solution:
  1. Step 1: Understand function logic

    The function multiplies all passed integers starting from 1.
  2. Step 2: Calculate multiplication

    2 * 3 = 6, 6 * 4 = 24, so result is 24.
  3. Final Answer:

    24 -> Option B
  4. Quick Check:

    Variadic multiplication returns product of all arguments [OK]
Quick Trick: Multiply all variadic arguments by looping [OK]
Common Mistakes:
  • Adding instead of multiplying
  • Expecting string concatenation
  • Thinking variadic cannot be used in calculations

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes