Consider an API endpoint that returns a list of items with pagination using limit and offset query parameters. The data source contains 10 items labeled from 1 to 10.
The API call is: GET /items?limit=3&offset=4
What list of item IDs will the API return?
items = list(range(1, 11)) limit = 3 offset = 4 result = items[offset:offset+limit] print(result)
Remember that list indexing in Python starts at 0, so offset 4 means the 5th item.
The offset 4 means start from the 5th item (index 4). With limit 3, the API returns items at indices 4, 5, and 6, which are 5, 6, and 7.
Choose the correct statement about using limit and offset for pagination in REST APIs.
Think about how you would get the next page of results.
Offset tells the API how many items to skip before starting to return results. Limit tells how many items to return after skipping.
Given the following code snippet for pagination, why does it return an empty list?
items = list(range(1, 21)) limit = 5 offset = 20 result = items[offset:offset+limit] print(result)
Check the length of the list and the value of offset.
The list has 20 items indexed 0 to 19. Offset 20 means start slicing at index 20, which is beyond the last item, so the slice returns an empty list.
Choose the code snippet that correctly returns a paginated list of items using limit and offset.
Remember slicing syntax is list[start:end] where start is inclusive and end is exclusive.
To get items starting at offset and up to limit items, slice from offset to offset+limit.
An API has 15 items. A client requests GET /items?limit=7&offset=10. How many items will the API return?
Calculate how many items remain after skipping the offset.
Offset 10 skips the first 10 items, leaving 5 items (15 - 10). Limit 7 means up to 7 items can be returned, but only 5 remain, so 5 items are returned.