0
0
Kotlinprogramming~10 mins

Vararg parameters in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a function that accepts a variable number of integers.

Kotlin
fun printNumbers([1] numbers: Int) {
    for (number in numbers) {
        println(number)
    }
}
Drag options to blanks, or click blank then click option'
Aval
Bvar
Cvararg
Dconst
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'val' or 'var' instead of 'vararg' for variable arguments.
Forgetting to add the keyword before the parameter name.
2fill in blank
medium

Complete the code to call the function with multiple integer arguments.

Kotlin
printNumbers([1])
Drag options to blanks, or click blank then click option'
A1, 2, 3
BlistOf(1, 2, 3)
CarrayOf(1, 2, 3)
DsetOf(1, 2, 3)
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a list or array directly without the spread operator.
Using a collection type instead of separate arguments.
3fill in blank
hard

Fix the error in calling the function with an array argument.

Kotlin
val nums = intArrayOf(4, 5, 6)
printNumbers([1] nums)
Drag options to blanks, or click blank then click option'
Anums
B*
C*@
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the array variable directly without the spread operator.
Using incorrect symbols like '*@' or repeating '*'.
4fill in blank
hard

Fill both blanks to define a function that accepts a string and a variable number of integers.

Kotlin
fun showDetails(name: String, [1] numbers: [2]) {
    println("Name: $name")
    for (num in numbers) {
        println(num)
    }
}
Drag options to blanks, or click blank then click option'
Avararg
BIntArray
CInt
DArray<Int>
Attempts:
3 left
💡 Hint
Common Mistakes
Using array types like IntArray or Array instead of the element type.
Omitting the vararg keyword.
5fill in blank
hard

Fill all three blanks to create a function that sums variable integers and returns the result.

Kotlin
fun sumNumbers([1] nums: [2]): [3] {
    var total = 0
    for (n in nums) {
        total += n
    }
    return total
}
Drag options to blanks, or click blank then click option'
Avararg
BInt
DDouble
Attempts:
3 left
💡 Hint
Common Mistakes
Using Double as return type when summing integers.
Omitting vararg keyword.