Pagination & Sorting in Spring Data — Concept Check
Questions on Pagination & Sorting in Spring Data. Pass Pageable to repository methods; Spring Data returns Page<T> with content, total elements, and page metadata; Sort can be constructed dynamically from user input.
Questions in this quiz
8 questions on Pagination & Sorting in Spring Data — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer uses Page<T> for an infinite scroll UI, but notices performance issues. What could be the cause?
- APage<T> does not support sorting, causing inefficiencies.
- BPage<T> cannot handle large datasets efficiently.
- CPage<T> automatically sorts data, leading to delays.
- DPage<T> performs a count query, which is unnecessary for infinite scroll.
2.A service method accepts a user-supplied sort field for a product search. What is a necessary step to ensure security?
- ADirectly pass the user input to Sort.by() for dynamic sorting.
- BSort fields do not need validation as they are harmless.
- CUse a default sort field if the user input is invalid.
- DValidate the sort field against a whitelist before using it.
3.How can you avoid in-memory pagination warnings when dealing with large collections joined with @OneToMany?
- ASwitch to using Slice<T> instead of Page<T>.
- BUse @EntityGraph or JOIN FETCH to optimize the query.
- CIncrease the page size to reduce the number of queries.
- DDisable pagination to prevent warnings.
4.A developer notices that their paginated query is slow. They are using Page<T> and suspect the count query is the issue. What should they consider using instead?
- AIncrease the page size to reduce the number of queries.
- BSwitch to Slice<T> to avoid the count query.
- CUse a different sorting field to improve performance.
- DImplement caching to speed up the query.
5.When constructing a Pageable object, which of the following is NOT a required parameter?
- APage number
- BSort order
- CEntity type
- DPage size
6.A developer uses dynamic sorting based on user input without validation. What risk does this pose?
- AIncorrect sort order due to user errors.
- BPerformance degradation due to complex queries.
- CInability to paginate results effectively.
- DSQL injection via malicious field names.
7.Which of the following best describes the difference between Page<T> and Slice<T>?
- APage<T> supports sorting; Slice<T> does not.
- BPage<T> is for small datasets; Slice<T> is for large datasets.
- CPage<T> includes a count query; Slice<T> does not.
- DPage<T> is faster than Slice<T> for all queries.
8.A developer wants to sort products by a user-supplied field. What is the best practice to prevent SQL injection?
- AAvoid sorting altogether to eliminate risks.
- BDirectly use the user input in Sort.by().
- COnly allow sorting by primary key fields.
- DUse a whitelist to validate the field name before sorting.