Bird
0
0

How can you modify this Swift function to accept both a variadic list of strings and an optional prefix string?

hard📝 Application Q9 of 15
Swift - Functions
How can you modify this Swift function to accept both a variadic list of strings and an optional prefix string?
func printPrefixed(_ strings: String...) {
    for s in strings {
        print(s)
    }
}
AAdd a second parameter with default value after the variadic parameter
BAdd a second parameter with default value before the variadic parameter
CMake the variadic parameter optional
DUse an array parameter instead of variadic
Step-by-Step Solution
Solution:
  1. Step 1: Recall parameter order rules

    Variadic parameter must be last, so optional prefix must come before it.
  2. Step 2: Modify function signature

    Adding optional prefix before variadic parameter with default value allows calling without prefix.
  3. Final Answer:

    Add a second parameter with default value before the variadic parameter -> Option B
  4. Quick Check:

    Variadic parameter must be last; optional params before it [OK]
Quick Trick: Place optional params before variadic parameter [OK]
Common Mistakes:
  • Placing optional parameter after variadic
  • Trying to make variadic optional
  • Replacing variadic with array unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes