Challenge - 5 Problems
Loop Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a while loop with decrement
What is the output of this Kotlin code snippet?
Kotlin
var x = 3 while (x > 0) { print(x) x-- }
Attempts:
2 left
💡 Hint
Remember that the loop runs while x is greater than zero and x decreases each time.
✗ Incorrect
The loop prints x starting from 3 down to 1, so the output is '321'.
❓ Predict Output
intermediate2:00remaining
Output of a do-while loop with increment
What will this Kotlin code print?
Kotlin
var count = 0 do { print(count) count += 2 } while (count < 5)
Attempts:
2 left
💡 Hint
The loop runs at least once and increments count by 2 each time.
✗ Incorrect
The loop prints 0, then increments count to 2, prints 2, increments to 4, prints 4, then increments to 6 and stops because 6 is not less than 5.
🔧 Debug
advanced2:00remaining
Identify the error in this while loop
What error does this Kotlin code produce?
Kotlin
var i = 0 while (i < 3) { println(i) }
Attempts:
2 left
💡 Hint
Check if the loop variable changes inside the loop.
✗ Incorrect
The variable i is never incremented, so the condition i < 3 is always true, causing an infinite loop.
❓ Predict Output
advanced2:00remaining
Output of nested while and do-while loops
What is the output of this Kotlin code?
Kotlin
var a = 1 while (a <= 2) { var b = 1 do { print(a * b) b++ } while (b <= 2) a++ }
Attempts:
2 left
💡 Hint
The outer loop runs twice, and the inner loop runs twice each time.
✗ Incorrect
For a=1, b=1 and 2 print 1 and 2; for a=2, b=1 and 2 print 2 and 4; combined output is '1224'.
🧠 Conceptual
expert2:00remaining
Difference between while and do-while loops
Which statement correctly describes the difference between while and do-while loops in Kotlin?
Attempts:
2 left
💡 Hint
Think about when the condition is checked in each loop type.
✗ Incorrect
While loops test the condition before running the loop body, so they may not run at all. Do-while loops run the body first, then test the condition, so they always run at least once.