Bird
Raised Fist0
Swiftprogramming~20 mins

Variadic parameters in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Variadic Parameters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a variadic sum function
What is the output of this Swift code that uses a variadic parameter to sum numbers?
Swift
func sum(_ numbers: Int...) -> Int {
    var total = 0
    for number in numbers {
        total += number
    }
    return total
}

print(sum(1, 2, 3, 4))
A0
B1234
C10
DError: variadic parameters cannot be used this way
Attempts:
2 left
💡 Hint
Think about how the function adds all the numbers passed in.
❓ Predict Output
intermediate
2:00remaining
Variadic parameters with no arguments
What will this Swift code print when calling a variadic function with no arguments?
Swift
func greet(names: String...) {
    print("Hello, \(names.count) people!")
}

greet()
ACompile error: missing arguments
BHello, 1 people!
CHello, nil people!
DHello, 0 people!
Attempts:
2 left
💡 Hint
Variadic parameters can be empty, so count will be zero.
🧠 Conceptual
advanced
2:00remaining
Understanding variadic parameter restrictions
Which statement about variadic parameters in Swift is true?
AVariadic parameters must be the last parameter in the function signature.
BA function can have multiple variadic parameters.
CVariadic parameters can only accept Int values.
DVariadic parameters automatically unwrap optional values.
Attempts:
2 left
💡 Hint
Think about the position of variadic parameters in function definitions.
❓ Predict Output
advanced
2:00remaining
Output of variadic function with mixed parameter types
What is the output of this Swift code using a variadic parameter and a normal parameter?
Swift
func describe(age: Int, hobbies: String...) {
    print("Age: \(age)")
    print("Hobbies count: \(hobbies.count)")
    for hobby in hobbies {
        print(hobby)
    }
}

describe(age: 30, hobbies: "Reading", "Swimming")
A
Age: 30
Hobbies count: 0
B
Age: 30
Hobbies count: 2
Reading
Swimming
C
Age: 30
Hobbies count: 1
Reading, Swimming
DCompile error: variadic parameter must be last
Attempts:
2 left
💡 Hint
Check how many hobbies are passed and printed.
❓ Predict Output
expert
2:00remaining
Result of passing an array to a variadic parameter
What will this Swift code print when passing an array to a variadic parameter using the spread operator?
Swift
func multiply(factor: Int, numbers: Int...) -> [Int] {
    numbers.map { $0 * factor }
}

let values = [1, 2, 3]
let result = multiply(factor: 2, numbers: values...)
print(result)
A[2, 4, 6]
B[1, 2, 3, 2]
C[4, 6, 8]
DError: cannot use '...' to expand array here
Attempts:
2 left
💡 Hint
The '...' operator expands the array elements as separate arguments.

Practice

(1/5)
1. What does a variadic parameter in Swift allow a function to do?
easy
A. Limit the number of arguments to exactly two
B. Accept any number of values of the same type
C. Accept only one value of any type
D. Return multiple values from a function

Solution

  1. Step 1: Understand variadic parameter purpose

    Variadic parameters let a function accept zero or more values of the same type.
  2. Step 2: Compare options with definition

    Only Accept any number of values of the same type correctly describes this behavior.
  3. Final Answer:

    Accept any number of values of the same type -> Option B
  4. Quick Check:

    Variadic parameter = multiple values [OK]
Hint: Variadic means 'variable number' of same type values [OK]
Common Mistakes:
  • Confusing variadic with multiple return values
  • Thinking it accepts different types
  • Assuming it limits arguments
2. Which of the following is the correct way to declare a variadic parameter in Swift?
easy
A. func sum(numbers: Int*) { }
B. func sum(numbers: [Int]) { }
C. func sum(numbers: Int...) { }
D. func sum(numbers: Int) { }

Solution

  1. Step 1: Recall variadic syntax in Swift

    Variadic parameters use three dots (...) after the type.
  2. Step 2: Check each option

    func sum(numbers: Int...) { } uses 'Int...' which is correct syntax for variadic parameters.
  3. Final Answer:

    func sum(numbers: Int...) { } -> Option C
  4. Quick Check:

    Variadic syntax = Type... [OK]
Hint: Look for three dots (...) after type for variadic [OK]
Common Mistakes:
  • Using asterisk (*) instead of three dots
  • Using array type instead of variadic
  • Missing dots after type
3. What is the output of this Swift code?
func greet(names: String...) {
    for name in names {
        print("Hello, \(name)!")
    }
}
greet(names: "Anna", "Bob")
medium
A. Hello, Anna!\nHello, Bob!
B. Hello, names!
C. Hello, Anna, Bob!
D. Error: Cannot use variadic parameter here

Solution

  1. Step 1: Understand variadic parameter behavior

    The parameter 'names' collects all arguments into an array of strings.
  2. Step 2: Trace the for loop output

    The loop prints "Hello, Anna!" and then "Hello, Bob!" each on a new line.
  3. Final Answer:

    Hello, Anna!\nHello, Bob! -> Option A
  4. Quick Check:

    Variadic parameter prints each argument separately [OK]
Hint: Variadic acts like array; loop prints each item [OK]
Common Mistakes:
  • Thinking it prints all names in one line
  • Assuming a syntax error
  • Confusing parameter name with output
4. Identify the error in this Swift function using variadic parameters:
func multiply(numbers: Int...) -> Int {
    var result = 1
    for i in 0..
medium
A. The for loop syntax is incorrect
B. Missing return statement in function
C. Variadic parameters cannot be used with Int type
D. Calling multiply without any arguments causes an error

Solution

  1. Step 1: Analyze function call

    The call 'multiply(numbers: )' passes no arguments, which is allowed but syntax requires no empty parentheses.
  2. Step 2: Understand Swift call syntax for variadic

    Calling with empty parentheses after variadic parameter name is invalid; omit arguments or provide values.
  3. Final Answer:

    Calling multiply without any arguments causes an error -> Option D
  4. Quick Check:

    Empty variadic call syntax invalid [OK]
Hint: Call variadic with values or omit parentheses if none [OK]
Common Mistakes:
  • Thinking for loop is wrong
  • Believing Int can't be variadic
  • Missing return statement error
5. How can you write a Swift function that accepts any number of integers and returns their average as a Double using variadic parameters?
hard
A. func average(numbers: Int...) -> Double { Double(numbers.reduce(0, +)) / Double(numbers.count) }
B. func average(numbers: [Int]) -> Double { Double(numbers.reduce(0, +)) / Double(numbers.count) }
C. func average(numbers: Int...) -> Int { numbers.reduce(0, +) / numbers.count }
D. func average(numbers: Int*) -> Double { Double(numbers.reduce(0, +)) / Double(numbers.count) }

Solution

  1. Step 1: Use variadic parameter syntax

    func average(numbers: Int...) -> Double { Double(numbers.reduce(0, +)) / Double(numbers.count) } correctly uses 'Int...' to accept any number of integers.
  2. Step 2: Calculate average as Double

    It sums all numbers and divides by count, converting to Double for decimal average.
  3. Final Answer:

    func average(numbers: Int...) -> Double { Double(numbers.reduce(0, +)) / Double(numbers.count) } -> Option A
  4. Quick Check:

    Variadic + Double average calculation = func average(numbers: Int...) -> Double { Double(numbers.reduce(0, +)) / Double(numbers.count) } [OK]
Hint: Use Int... and convert sum/count to Double for average [OK]
Common Mistakes:
  • Using array instead of variadic parameter
  • Returning Int instead of Double for average
  • Using invalid syntax like Int*
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard