Bird
Raised Fist0
Spring Bootframework~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of using Pageable in Spring Boot?
easy
A. To fetch data in small chunks with optional sorting
B. To connect to the database directly
C. To write SQL queries manually
D. To handle user authentication

Solution

  1. Step 1: Understand Pageable's role

    Pageable is used to request data in pages, not all at once.
  2. Step 2: Recognize sorting feature

    It also supports sorting data by fields while fetching pages.
  3. Final Answer:

    To fetch data in small chunks with optional sorting -> Option A
  4. Quick Check:

    Pageable = Pagination + Sorting [OK]
Hint: Pageable means small pages + sorting [OK]
Common Mistakes:
  • Thinking Pageable connects to database
  • Confusing Pageable with authentication
  • Assuming Pageable writes SQL queries
2. Which of the following is the correct way to create a Pageable object for page 2, size 5, sorted by "name" ascending?
easy
A. PageRequest.of(1, 5, Sort.by("name"))
B. PageRequest.of(2, 5, Sort.asc("name"))
C. PageRequest.of(2, 5, Sort.by("name"))
D. PageRequest.create(2, 5, Sort.by("name"))

Solution

  1. Step 1: Understand zero-based page index

    Page numbers start at 0, so page 2 means index 1.
  2. Step 2: Check method and sorting syntax

    Use PageRequest.of(page, size, Sort.by("field")) for ascending sort.
  3. Final Answer:

    PageRequest.of(1, 5, Sort.by("name")) -> Option A
  4. Quick Check:

    Page index zero-based + Sort.by correct [OK]
Hint: Page index starts at 0, so page 2 is index 1 [OK]
Common Mistakes:
  • Using page number directly instead of zero-based index
  • Using non-existent methods like Sort.asc
  • Using PageRequest.create instead of PageRequest.of
3. Given this repository method call:
repository.findAll(PageRequest.of(0, 3, Sort.by("age")))
What will be the result?
medium
A. Error because Sort direction is missing
B. First 3 records sorted by age descending
C. All records without sorting
D. First 3 records sorted by age ascending

Solution

  1. Step 1: Analyze PageRequest parameters

    Page 0 means first page, size 3 means 3 records, Sort.by("age") defaults to ascending.
  2. Step 2: Understand repository behavior

    findAll with Pageable returns that page of sorted data.
  3. Final Answer:

    First 3 records sorted by age ascending -> Option D
  4. Quick Check:

    Page 0 + size 3 + ascending sort = first 3 sorted [OK]
Hint: Sort.by defaults to ascending if direction not set [OK]
Common Mistakes:
  • Assuming descending sort without direction
  • Thinking all records are returned
  • Expecting error due to missing direction
4. What is wrong with this code snippet?
Pageable pageable = PageRequest.of(1, 10, Sort.asc("date"));
medium
A. Page index should start at 0, not 1
B. Sort.asc() method does not exist
C. Page size cannot be 10
D. PageRequest.of requires 4 parameters

Solution

  1. Step 1: Check Sort method usage

    Spring Data uses Sort.by(), not Sort.asc().
  2. Step 2: Verify other parameters

    Page index 1 and size 10 are valid; PageRequest.of takes 3 parameters here.
  3. Final Answer:

    Sort.asc() method does not exist -> Option B
  4. Quick Check:

    Use Sort.by() for sorting [OK]
Hint: Use Sort.by(), not Sort.asc() [OK]
Common Mistakes:
  • Using Sort.asc() or Sort.desc() which don't exist
  • Thinking page index must be 0 always
  • Believing PageRequest.of needs 4 parameters
5. You want to fetch the third page of size 4, sorted by "price" descending and then by "name" ascending. Which Pageable creation is correct?
hard
A. PageRequest.of(2, 4, Sort.by("price", "name"))
B. PageRequest.of(3, 4, Sort.by("price").descending().and(Sort.by("name")))
C. PageRequest.of(2, 4, Sort.by(Sort.Order.desc("price"), Sort.Order.asc("name")))
D. PageRequest.of(2, 4, Sort.by("price").desc().and(Sort.by("name").asc()))

Solution

  1. Step 1: Identify zero-based page index

    Third page means index 2 (0,1,2).
  2. Step 2: Create Sort with multiple orders

    Use Sort.by(Order.desc("price"), Order.asc("name")) to combine sorting directions.
  3. Step 3: Check syntax correctness

    PageRequest.of(2, 4, Sort.by(Sort.Order.desc("price"), Sort.Order.asc("name"))) uses correct PageRequest.of and Sort.by with orders.
  4. Final Answer:

    PageRequest.of(2, 4, Sort.by(Sort.Order.desc("price"), Sort.Order.asc("name"))) -> Option C
  5. Quick Check:

    Page 2 + size 4 + multi-sort orders correct [OK]
Hint: Use Sort.Order for multi-direction sorting [OK]
Common Mistakes:
  • Using page number 3 instead of index 2
  • Trying to chain .desc() or .asc() methods that don't exist
  • Passing multiple fields without specifying order