Bird
0
0

What will be the output of this Swift code?

medium📝 Predict Output Q4 of 15
Swift - Functions
What will be the output of this Swift code?
func printAll(_ strings: String...) {
    for str in strings {
        print(str)
    }
}
printAll("apple", "banana", "cherry")
Aapple banana cherry
B["apple", "banana", "cherry"]
Capple banana cherry
DError: Cannot pass multiple arguments
Step-by-Step Solution
Solution:
  1. Step 1: Understand function behavior

    The function takes multiple strings and prints each on a new line using a loop.
  2. Step 2: Analyze output

    Each string is printed separately, so output is apple, banana, cherry each on its own line.
  3. Final Answer:

    apple banana cherry -> Option A
  4. Quick Check:

    Variadic prints each argument on separate lines [OK]
Quick Trick: Variadic loops print each argument separately [OK]
Common Mistakes:
  • Expecting array print format
  • Expecting all strings on one line
  • Thinking multiple arguments cause error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes