0
0
Kotlinprogramming~10 mins

Repeat function for simple repetition 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 repeat the print statement 3 times using the repeat function.

Kotlin
repeat([1]) {
    println("Hello")
}
Drag options to blanks, or click blank then click option'
A3
B5
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 will not print anything.
Using 1 will print only once, not three times.
2fill in blank
medium

Complete the code to print the numbers 0 to 4 using repeat and the loop index.

Kotlin
repeat(5) { [1] ->
    println([1])
}
Drag options to blanks, or click blank then click option'
Anum
Bcount
Cindex
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not declared in the lambda.
Forgetting to use the lambda parameter to print the index.
3fill in blank
hard

Fix the error in the code to correctly repeat printing "Hi" 4 times.

Kotlin
repeat([1]) {
    println("Hi")
}
Drag options to blanks, or click blank then click option'
Afour
B4
C"4"
D4L
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string like "4" causes a type error.
Using a variable name that is not defined.
4fill in blank
hard

Fill both blanks to create a map of numbers to their squares using repeat.

Kotlin
val squares = mutableMapOf<Int, Int>()
repeat([1]) { [2] ->
    squares[[2]] = [2] * [2]
}
Drag options to blanks, or click blank then click option'
A5
Bi
Cindex
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using a number too large or too small for the repeat count.
Using a variable name not matching the lambda parameter.
5fill in blank
hard

Fill all three blanks to create a list of even numbers using repeat and an if condition.

Kotlin
val evens = mutableListOf<Int>()
repeat([1]) { [2] ->
    if ([2] % 2 == 0) {
        evens.add([2])
    }
}
Drag options to blanks, or click blank then click option'
A10
Bi
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for the loop index in condition and add.
Repeating fewer times than needed to get all even numbers.