0
0
Android Kotlinmobile~20 mins

Side effects (LaunchedEffect, SideEffect) in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Side Effects Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when LaunchedEffect key changes?

Consider this Compose snippet:

var count by remember { mutableStateOf(0) }
LaunchedEffect(count) {
  println("Count is $count")
}

What happens when count changes?

Android Kotlin
var count by remember { mutableStateOf(0) }
LaunchedEffect(count) {
  println("Count is $count")
}
AThe block runs only once when the composable is first composed.
BThe block inside LaunchedEffect restarts and prints the new count value.
CThe block never runs because count is mutable.
DThe block runs repeatedly in an infinite loop.
Attempts:
2 left
💡 Hint

LaunchedEffect runs its block when the key changes.

lifecycle
intermediate
2:00remaining
When does SideEffect run in Compose?

Given this Compose code:

SideEffect {
  println("SideEffect executed")
}

When is this block executed?

Android Kotlin
SideEffect {
  println("SideEffect executed")
}
AAfter every successful recomposition of the composable.
BOnly once when the composable is first composed.
CBefore the composable is composed.
DOnly when the composable is disposed.
Attempts:
2 left
💡 Hint

Think about when SideEffect is designed to run in the Compose lifecycle.

🔧 Debug
advanced
2:00remaining
Why does this LaunchedEffect cause repeated prints?

Look at this code:

var text by remember { mutableStateOf("") }
LaunchedEffect(Unit) {
  while(true) {
    delay(1000)
    println("Tick")
  }
}
Button(onClick = { text += "a" }) {
  Text(text)
}

What is the problem with this LaunchedEffect?

Android Kotlin
var text by remember { mutableStateOf("") }
LaunchedEffect(Unit) {
  while(true) {
    delay(1000)
    println("Tick")
  }
}
Button(onClick = { text += "a" }) {
  Text(text)
}
AThe LaunchedEffect never runs because Unit is not a valid key.
BThe LaunchedEffect restarts on every recomposition causing multiple "Tick" prints per second.
CThe LaunchedEffect runs only once and prints "Tick" every second as expected.
DThe Button click causes the LaunchedEffect to cancel immediately.
Attempts:
2 left
💡 Hint

Consider what key Unit means for LaunchedEffect.

🧠 Conceptual
advanced
2:00remaining
Difference between LaunchedEffect and SideEffect?

Which statement correctly describes the difference between LaunchedEffect and SideEffect in Jetpack Compose?

ALaunchedEffect runs suspend functions and can launch coroutines; SideEffect runs synchronously after recomposition.
BLaunchedEffect runs only once; SideEffect runs only on disposal.
CBoth run suspend functions but LaunchedEffect cancels on key change; SideEffect does not.
DSideEffect launches coroutines; LaunchedEffect runs synchronously after recomposition.
Attempts:
2 left
💡 Hint

Think about coroutine support and timing of execution.

📝 Syntax
expert
2:00remaining
What error occurs with this LaunchedEffect usage?

Analyze this code snippet:

LaunchedEffect {
  println("Hello")
}

What error will this cause?

Android Kotlin
LaunchedEffect {
  println("Hello")
}
AType error: LaunchedEffect expects a suspend lambda.
BRuntime error: LaunchedEffect block cannot print to console.
CSyntax error: Missing key parameter in LaunchedEffect call.
DNo error: This code runs fine and prints "Hello" once.
Attempts:
2 left
💡 Hint

LaunchedEffect(vararg keys: Any?, block: suspend CoroutineScope.() -> Unit); vararg keys can be omitted.