0
0
Kotlinprogramming~20 mins

Repeat function for simple repetition in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Repeat Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of repeat function with print
What is the output of this Kotlin code using the repeat function?
Kotlin
fun main() {
    repeat(3) {
        print("Hi")
    }
}
A3
B
Hi
Hi
Hi
CHiHiHi
DHi Hi Hi
Attempts:
2 left
💡 Hint
The repeat function runs the block multiple times without adding new lines automatically.
Predict Output
intermediate
2:00remaining
Value of variable after repeat loop
What is the value of sum after running this Kotlin code?
Kotlin
fun main() {
    var sum = 0
    repeat(4) { i ->
        sum += i
    }
    println(sum)
}
A6
B10
C4
D0
Attempts:
2 left
💡 Hint
The repeat function passes the current iteration index starting from 0.
🔧 Debug
advanced
2:00remaining
Identify the error in repeat usage
What error does this Kotlin code produce?
Kotlin
fun main() {
    repeat(3)
        println("Hello")
    println("Done")
}
ASyntaxError: Missing braces for repeat block
BNo error, prints Hello three times then Done
CRuntimeException: repeat count must be positive
DCompilation error: Indentation error
Attempts:
2 left
💡 Hint
In Kotlin, if the block is a single statement, braces are optional.
Predict Output
advanced
2:00remaining
Output with nested repeat calls
What is the output of this Kotlin code with nested repeat calls?
Kotlin
fun main() {
    repeat(2) { i ->
        repeat(2) { j ->
            print("${i}${j} ")
        }
    }
}
A00 01 10 11
B00 10 01 11
C0 1 0 1
D01 00 11 10
Attempts:
2 left
💡 Hint
The outer loop runs for i=0,1 and inner for j=0,1 printing pairs.
🧠 Conceptual
expert
2:00remaining
Repeat function behavior with zero count
What happens when you call repeat(0) in Kotlin?
AThe block runs once with index 0
BRuns infinitely
CThrows IllegalArgumentException
DThe block does not run at all
Attempts:
2 left
💡 Hint
Think about how many times the block should run if count is zero.