Imagine you have a REST API that returns a list of thousands of items. Why is pagination important in this case?
Think about what happens if you try to send thousands of items at once.
Pagination splits large data into smaller chunks. This makes responses faster and uses less memory on both server and client.
Given this simplified API response code snippet, what will be the output when requesting page 2 with page size 3?
data = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] page = 2 page_size = 3 start = (page - 1) * page_size end = start + page_size result = data[start:end] print(result)
Calculate the start and end indexes carefully.
Page 2 with size 3 means items from index 3 to 5 (0-based). These are 'd', 'e', 'f'.
What error will this pagination code cause when requesting page 0?
def get_page(data, page, size): start = (page - 1) * size end = start + size return data[start:end] items = ['x', 'y', 'z'] print(get_page(items, 0, 2))
Think about how Python handles negative indexes in slicing.
page=0 gives start=-2 (effective index 1 in 0-based list of length 3), end=0. Slice data[1:0] returns [].
Which option will cause a syntax error when parsing pagination parameters in Python?
def parse_params(params): page = int(params.get('page', 1)) size = int(params.get('size', 10)) return page, size
Look carefully at the syntax of the get() method.
Option A misses a comma between 'page' and 1, causing a syntax error.
You have 53 items and want to paginate with 10 items per page. How many pages are needed?
Divide total items by page size and round up.
53 divided by 10 is 5.3, so you need 6 pages to cover all items.