0
0
Android Kotlinmobile~20 mins

LazyColumn with items in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LazyColumn Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the output of this LazyColumn code?

Consider this Kotlin Compose code snippet using LazyColumn:

LazyColumn {
  items(listOf("Apple", "Banana", "Cherry")) { fruit ->
    Text(text = fruit)
  }
}

What will the user see on the screen?

Android Kotlin
LazyColumn {
  items(listOf("Apple", "Banana", "Cherry")) { fruit ->
    Text(text = fruit)
  }
}
AA vertical scrollable list showing the words: Apple, Banana, Cherry each on its own line.
BA horizontal scrollable list showing the words: Apple, Banana, Cherry in a row.
CA single Text showing all fruits concatenated: "AppleBananaCherry".
DAn empty screen with no text visible.
Attempts:
2 left
💡 Hint

Remember, LazyColumn creates a vertical list by default.

📝 Syntax
intermediate
2:00remaining
Which option correctly uses LazyColumn with items?

Which of these Kotlin Compose snippets correctly compiles and displays a list of numbers 1 to 3?

A
LazyColumn {
  item(listOf(1, 2, 3)) { number ->
    Text(text = number.toString())
  }
}
B
LazyColumn {
  items(listOf(1, 2, 3)) { number ->
    Text(text = number.toString())
  }
}
C
LazyColumn {
  items(1..3) { number ->
    Text(text = number.toString())
  }
}
D
LazyColumn {
  items(listOf(1, 2, 3)) { number ->
    Text(number.toString())
  }
}
Attempts:
2 left
💡 Hint

Check the function name and parameter types carefully.

lifecycle
advanced
2:00remaining
What happens if the list passed to LazyColumn changes?

Given this code:

val fruits = remember { mutableStateListOf("Apple", "Banana") }

LazyColumn {
  items(fruits) { fruit ->
    Text(text = fruit)
  }
}

// Later fruits.add("Cherry") is called

What will happen in the UI after adding "Cherry" to fruits?

Android Kotlin
val fruits = remember { mutableStateListOf("Apple", "Banana") }

LazyColumn {
  items(fruits) { fruit ->
    Text(text = fruit)
  }
}

// fruits.add("Cherry") called later
AOnly the first two items will show; Cherry will be ignored.
BThe UI will not update until the whole composable is recomposed manually.
CThe app will crash because the list is modified after composition.
DThe UI will automatically update and show Apple, Banana, and Cherry in the list.
Attempts:
2 left
💡 Hint

Think about how Compose tracks changes in mutableStateListOf.

navigation
advanced
2:00remaining
How to handle item clicks in LazyColumn?

You want to show a list of names in a LazyColumn. When a user taps a name, you want to navigate to a detail screen with that name.

Which code snippet correctly handles item clicks?

A
LazyColumn {
  items(names) { name ->
    Text(text = name, modifier = Modifier.clickable { navigateToDetail(name) })
  }
}
B
LazyColumn {
  items(names) { name ->
    Text(text = name)
  }
  clickable { navigateToDetail(name) }
}
C
LazyColumn {
  items(names) { name ->
    Text(text = name)
  }
}

Modifier.clickable { navigateToDetail(name) }
D
LazyColumn {
  items(names) { name ->
    Text(text = name, onClick = { navigateToDetail(name) })
  }
}
Attempts:
2 left
💡 Hint

Remember how to add click behavior to composables using modifiers.

🔧 Debug
expert
2:00remaining
Why does this LazyColumn cause a runtime error?

Examine this code:

val items = listOf("One", "Two", "Three")

LazyColumn {
  items(items.size) { index ->
    Text(text = items[index])
  }
}

When running, the app crashes with an IndexOutOfBoundsException. Why?

Android Kotlin
val items = listOf("One", "Two", "Three")

LazyColumn {
  items(items.size) { index ->
    Text(text = items[index])
  }
}
ABecause the index goes out of bounds since items.size is not the correct count.
BBecause items(size) is valid but the lambda parameter should be named differently.
CBecause items(size) expects a list, not an integer size, causing wrong iteration.
DBecause the list is immutable and cannot be used in LazyColumn.
Attempts:
2 left
💡 Hint

Check the expected parameter types for items in LazyColumn.