Bird
0
0

What will be printed by this Swift code?

medium📝 Predict Output Q4 of 15
iOS Swift - Swift Language Essentials
What will be printed by this Swift code?
let add = { (a: Int, b: Int) -> Int in a + b }
print(add(3, 4))
Anil
B34
CError: Missing return
D7
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the closure definition

    The closure takes two Ints and returns their sum using 'a + b'.
  2. Step 2: Evaluate the print statement

    Calling add(3, 4) returns 7, which is printed.
  3. Final Answer:

    7 -> Option D
  4. Quick Check:

    Closure output = 7 [OK]
Quick Trick: Closures return values like functions [OK]
Common Mistakes:
  • Thinking 'a + b' concatenates numbers
  • Expecting a syntax error due to missing return keyword (it's implicit)
  • Confusing output with string concatenation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes