Challenge - 5 Problems
Pageable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Pageable request?
Given a Spring Data repository method call with Pageable, what page content is returned?
Spring Boot
Pageable pageable = PageRequest.of(1, 3, Sort.by("name").ascending()); Page<User> page = userRepository.findAll(pageable); List<User> users = page.getContent(); return users.stream().map(User::getName).toList();
Attempts:
2 left
💡 Hint
Remember page index starts at 0, and sorting is ascending by name.
✗ Incorrect
The Pageable requests page 1 (second page) with 3 items per page, sorted by name ascending. So the first page (index 0) has the first 3 names alphabetically, the second page has the next 3.
📝 Syntax
intermediate2:00remaining
Which Pageable creation code is correct for descending sort by age?
Choose the correct way to create a Pageable object that requests page 0, 5 items per page, sorted by age descending.
Attempts:
2 left
💡 Hint
Check the official PageRequest factory method and Sort API.
✗ Incorrect
PageRequest.of is the correct factory method. Sort.by("age").descending() creates the descending sort.
❓ state_output
advanced2:00remaining
What is the total number of pages for this Pageable query?
Assuming the database has 23 total items, what is the total number of pages returned by this Pageable query?
Spring Boot
Pageable pageable = PageRequest.of(0, 5); Page<Item> page = itemRepository.findAll(pageable); int totalPages = page.getTotalPages();
Attempts:
2 left
💡 Hint
Divide total items by page size and round up.
✗ Incorrect
23 items with 5 per page means 4 pages fully filled (5*4=20) plus 1 page with 3 items, total 5 pages. 23/5 = 4.6, rounded up is 5 pages.
🔧 Debug
advanced2:00remaining
Why does this Pageable query throw an exception?
This code throws an IllegalArgumentException. What is the cause?
Spring Boot
Pageable pageable = PageRequest.of(-1, 10); Page<Product> page = productRepository.findAll(pageable);
Attempts:
2 left
💡 Hint
Check the parameters for PageRequest.of.
✗ Incorrect
PageRequest.of requires a non-negative page index. Negative page index causes IllegalArgumentException.
🧠 Conceptual
expert3:00remaining
How does Spring Data handle sorting when multiple Sort.Order objects are provided in Pageable?
If a Pageable is created with Sort.by(Order.asc("name"), Order.desc("age")), how will the sorting be applied?
Attempts:
2 left
💡 Hint
Think about how multi-level sorting works in databases.
✗ Incorrect
Spring Data applies sorting in the order the Sort.Order objects are given. The first order is primary, the second is secondary.