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:
func add(_ a: Int, _ b: Int) -> Int {
    return a + b
}

let sum = add(3, 4)
let operation = add(3, 4)
print(operation(5, 6))
AFunction add is not defined properly
BMissing return statement in add function
CCannot assign function call to a constant
DCannot call operation as a function because it holds an Int, not a function
Step-by-Step Solution
Solution:
  1. Step 1: Analyze variable assignments

    sum is assigned the result of add(3, 4), which is an Int (7). operation is also assigned add(3, 4), so operation is an Int, not a function.
  2. Step 2: Check the call to operation(5, 6)

    Since operation is an Int, calling operation(5, 6) causes an error because Int is not callable.
  3. Final Answer:

    Cannot call operation as a function because it holds an Int, not a function -> Option D
  4. Quick Check:

    Calling Int as function causes error = Cannot call operation as a function because it holds an Int, not a function [OK]
Quick Trick: Assign function name, not function call, to variable [OK]
Common Mistakes:
  • Assigning function call instead of function reference
  • Expecting function call to be callable again
  • Ignoring type mismatch errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes