0
0
Kotlinprogramming~20 mins

While and do-while loops in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Loop Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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--
}
A123
B321
C3
D0
Attempts:
2 left
💡 Hint
Remember that the loop runs while x is greater than zero and x decreases each time.
Predict Output
intermediate
2: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)
A024
B0246
C02
D0
Attempts:
2 left
💡 Hint
The loop runs at least once and increments count by 2 each time.
🔧 Debug
advanced
2:00remaining
Identify the error in this while loop
What error does this Kotlin code produce?
Kotlin
var i = 0
while (i < 3) {
    println(i)
}
AInfinite loop (no increment of i)
BSyntax error: missing parentheses
CType error: i is not an integer
DNo error, prints 0 1 2
Attempts:
2 left
💡 Hint
Check if the loop variable changes inside the loop.
Predict Output
advanced
2: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++
}
A124
B1122
C1224
D2244
Attempts:
2 left
💡 Hint
The outer loop runs twice, and the inner loop runs twice each time.
🧠 Conceptual
expert
2:00remaining
Difference between while and do-while loops
Which statement correctly describes the difference between while and do-while loops in Kotlin?
AWhile loops can only count up; do-while loops can only count down.
BA while loop runs the body once before checking the condition; a do-while loop checks the condition first.
CBoth loops check the condition before running the body, but do-while loops run faster.
DA while loop checks the condition before running the loop body; a do-while loop runs the body once before checking the condition.
Attempts:
2 left
💡 Hint
Think about when the condition is checked in each loop type.