0
0
Spring Bootframework~20 mins

Pagination and sorting with Pageable in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pageable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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();
A["David", "Eve", "Frank"]
B["Alice", "Bob", "Charlie"]
C["Grace", "Heidi", "Ivan"]
D["Jack", "Karl", "Liam"]
Attempts:
2 left
💡 Hint
Remember page index starts at 0, and sorting is ascending by name.
📝 Syntax
intermediate
2: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.
APageable pageable = PageRequest.of(0, 5, Sort.descending("age"));
BPageable pageable = PageRequest.of(0, 5, Sort.by("age").descending());
CPageable pageable = new Pageable(0, 5, Sort.by("age").desc());
DPageable pageable = PageRequest.create(0, 5, Sort.by("age").descending());
Attempts:
2 left
💡 Hint
Check the official PageRequest factory method and Sort API.
state_output
advanced
2: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();
A5
B4
C7
D6
Attempts:
2 left
💡 Hint
Divide total items by page size and round up.
🔧 Debug
advanced
2: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);
ARepository method findAll does not accept Pageable.
BPage size cannot be 10; must be less than 10.
CPage index cannot be negative; -1 is invalid.
DSort parameter is missing, causing exception.
Attempts:
2 left
💡 Hint
Check the parameters for PageRequest.of.
🧠 Conceptual
expert
3: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?
ASorts only by the last Order specified, ignoring the first.
BSorts first by age descending, then by name ascending for items with the same age.
CThrows an exception because multiple Sort.Order objects are not allowed.
DSorts first by name ascending, then by age descending for items with the same name.
Attempts:
2 left
💡 Hint
Think about how multi-level sorting works in databases.