0
0
Android Kotlinmobile~10 mins

Flow basics in Android 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 create a simple Flow that emits numbers from 1 to 3.

Android Kotlin
val numbersFlow = flow {
    for (i in 1..[1]) {
        emit(i)
    }
}
Drag options to blanks, or click blank then click option'
A0
B5
C10
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 as the upper bound results in no emissions.
Using 5 or 10 emits more numbers than required.
2fill in blank
medium

Complete the code to collect and print each value emitted by the Flow.

Android Kotlin
numbersFlow.[1] { value ->
    println(value)
}
Drag options to blanks, or click blank then click option'
Amap
Bcollect
ClaunchIn
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using launchIn without a CoroutineScope causes errors.
Using map or filter does not collect values.
3fill in blank
hard

Fix the error in the code to emit strings instead of integers.

Android Kotlin
val stringFlow = flow {
    for (i in 1..3) {
        emit([1])
    }
}
Drag options to blanks, or click blank then click option'
Ai.toString()
Bi
C"i"
Di + ""
Attempts:
3 left
💡 Hint
Common Mistakes
Emitting i emits integers, not strings.
Using "i" emits the literal letter i, not the number.
4fill in blank
hard

Fill both blanks to create a Flow that emits only even numbers from 1 to 5.

Android Kotlin
val evenFlow = flow {
    for (num in 1..5) {
        if (num [1] 2 == 0) {
            emit(num[2])
        }
    }
}
Drag options to blanks, or click blank then click option'
A%
B*
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using * or + in the if condition causes wrong checks.
Adding operators after emit(num) causes syntax errors.
5fill in blank
hard

Fill all three blanks to create a Flow that emits squares of numbers greater than 2 from 1 to 4.

Android Kotlin
val squaresFlow = flow {
    for (x in 1..4) {
        if (x [1] 2) {
            emit(x [2] x)
        }
    }
}

squaresFlow.collect { value ->
    println(value [3] 0)
}
Drag options to blanks, or click blank then click option'
A>
B*
C>=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using >= instead of > changes the condition.
Using == in the print condition prints false instead of true.