Bird
0
0

What will the following Swift code output?

medium📝 Predict Output Q13 of 15
Swift - Basics and Runtime
What will the following Swift code output?
/// Adds two numbers
func add(_ a: Int, _ b: Int) -> Int {
    return a + b
}
print(add(3, 4))
A7
Badd(3, 4)
C/// Adds two numbers
DError: Unexpected documentation comment
Step-by-Step Solution
Solution:
  1. Step 1: Understand documentation comments effect

    The line starting with /// is a documentation comment and does not affect the code execution.
  2. Step 2: Execute the function call

    The function add returns the sum of 3 and 4, which is 7, and print outputs it.
  3. Final Answer:

    7 -> Option A
  4. Quick Check:

    Documentation comment ignored, output = 7 [OK]
Quick Trick: Documentation comments don't change output, only explain [OK]
Common Mistakes:
  • Thinking documentation comments print text
  • Expecting error from documentation comments
  • Confusing comments with code

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes