0
0
Android Kotlinmobile~20 mins

Pagination basics in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pagination Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when you scroll to the bottom of this RecyclerView?
Given this Kotlin code snippet for a RecyclerView with pagination, what is the expected behavior when the user scrolls to the bottom?
Android Kotlin
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
  override fun onScrolled(rv: RecyclerView, dx: Int, dy: Int) {
    super.onScrolled(rv, dx, dy)
    val layoutManager = rv.layoutManager as LinearLayoutManager
    val totalItemCount = layoutManager.itemCount
    val lastVisible = layoutManager.findLastVisibleItemPosition()
    if (lastVisible == totalItemCount - 1) {
      loadNextPage()
    }
  }
})
AThe app loads the next page of data by calling loadNextPage() when the last item is visible.
BThe app refreshes the entire list from the first page when the bottom is reached.
CNothing happens because onScrolled is not triggered at the bottom.
DThe app crashes because findLastVisibleItemPosition() is not a valid method.
Attempts:
2 left
💡 Hint
Think about what happens when the last visible item matches the total items minus one.
🧠 Conceptual
intermediate
1:30remaining
Why use pagination in mobile apps?
Which of these is the main reason to implement pagination in a mobile app?
ATo make the UI more colorful and attractive.
BTo reduce memory usage and improve performance by loading data in chunks.
CTo load all data at once for faster access.
DTo prevent users from scrolling too fast.
Attempts:
2 left
💡 Hint
Think about how loading large data sets affects app speed and memory.
📝 Syntax
advanced
1:00remaining
What is the output of this Kotlin code for a paginated list?
Consider this code snippet that appends new items to a list during pagination. What will be the size of 'items' after running?
Android Kotlin
val items = mutableListOf(1, 2, 3)
val newItems = listOf(4, 5)
items.addAll(newItems)
println(items.size)
ACompilation error
B3
C2
D5
Attempts:
2 left
💡 Hint
What does addAll() do to the list?
lifecycle
advanced
1:30remaining
When should you reset pagination state in an Android app?
In an app with pagination, when is it best to reset the current page number and clear loaded data?
AWhen the user performs a new search or refreshes the list.
BOnly when the app is closed and reopened.
CNever, keep loading pages continuously without reset.
DWhen the device orientation changes.
Attempts:
2 left
💡 Hint
Think about when the data source changes completely.
🔧 Debug
expert
2:30remaining
Why does this pagination code cause duplicate items?
This Kotlin code appends new page data but sometimes shows duplicates. Why?
Android Kotlin
fun loadNextPage() {
  val newData = fetchData(page)
  items.addAll(newData)
  adapter.notifyDataSetChanged()
  page += 1
}

// fetchData returns the same data for page 1 and 2 by mistake
Aadapter.notifyDataSetChanged() removes old items causing duplicates.
Bitems.addAll(newData) clears the list before adding, so duplicates appear.
CfetchData returns duplicate data for different pages, causing duplicates in 'items'.
Dpage variable is never incremented, so same page loads repeatedly.
Attempts:
2 left
💡 Hint
Check what fetchData returns for each page.