Bird
0
0

What is the output of this Swift code?

medium📝 Predict Output Q4 of 15
Swift - Functions
What is the output of this Swift code?
func divide(_ a: Int, by b: Int) -> Int {
    return a / b
}

print(divide(10, by: 2))
ARuntime error
B10
CSyntax error
D5
Step-by-Step Solution
Solution:
  1. Step 1: Understand function parameters and call

    The first parameter uses _ so no label needed; second parameter label is by.
  2. Step 2: Evaluate the function call and output

    Calling divide(10, by: 2) divides 10 by 2, returning 5, which is printed.
  3. Final Answer:

    5 -> Option D
  4. Quick Check:

    Underscore omits label for first param; output = 5 [OK]
Quick Trick: Use _ to omit label; keep others as is [OK]
Common Mistakes:
  • Trying to use label for first parameter
  • Confusing parameter labels in call
  • Expecting syntax or runtime errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes