Challenge - 5 Problems
Repeat Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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") } }
Attempts:
2 left
💡 Hint
The
repeat function runs the block multiple times without adding new lines automatically.✗ Incorrect
The
repeat(3) runs the print statement 3 times consecutively. Since print does not add a newline, the output is 'HiHiHi'.❓ Predict Output
intermediate2: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) }
Attempts:
2 left
💡 Hint
The
repeat function passes the current iteration index starting from 0.✗ Incorrect
The loop runs 4 times with i = 0,1,2,3. Sum is 0+1+2+3 = 6.
🔧 Debug
advanced2:00remaining
Identify the error in repeat usage
What error does this Kotlin code produce?
Kotlin
fun main() { repeat(3) println("Hello") println("Done") }
Attempts:
2 left
💡 Hint
In Kotlin, if the block is a single statement, braces are optional.
✗ Incorrect
The repeat function accepts a single statement without braces. It prints 'Hello' three times, then 'Done'.
❓ Predict Output
advanced2: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} ") } } }
Attempts:
2 left
💡 Hint
The outer loop runs for i=0,1 and inner for j=0,1 printing pairs.
✗ Incorrect
The code prints pairs of i and j for each nested loop iteration: 00 01 10 11
🧠 Conceptual
expert2:00remaining
Repeat function behavior with zero count
What happens when you call
repeat(0) in Kotlin?Attempts:
2 left
💡 Hint
Think about how many times the block should run if count is zero.
✗ Incorrect
Calling repeat with 0 means the block runs zero times, so it does not execute at all.