Consider a REST API that returns paginated results. The following Python code generates pagination links for a given page and total pages.
def get_pagination_links(current_page, total_pages):
base_url = 'https://api.example.com/items?page='
links = {}
if current_page > 1:
links['prev'] = base_url + str(current_page - 1)
if current_page < total_pages:
links['next'] = base_url + str(current_page + 1)
links['self'] = base_url + str(current_page)
return links
print(get_pagination_links(3, 5))What will be printed?
def get_pagination_links(current_page, total_pages): base_url = 'https://api.example.com/items?page=' links = {} if current_page > 1: links['prev'] = base_url + str(current_page - 1) if current_page < total_pages: links['next'] = base_url + str(current_page + 1) links['self'] = base_url + str(current_page) return links print(get_pagination_links(3, 5))
Think about which pages exist before and after the current page 3 when total pages are 5.
The code adds a 'prev' link only if the current page is greater than 1, so page 2 is included. It adds a 'next' link only if current page is less than total pages, so page 4 is included. The 'self' link is always included for the current page 3.
Look at this JavaScript function that generates pagination links:
function getPaginationLinks(currentPage, totalPages) {
const baseUrl = 'https://api.example.com/items?page=';
const links = {};
if (currentPage > 1) {
links.prev = baseUrl + (currentPage - 1);
}
if (currentPage < totalPages) {
links.next = baseUrl + (currentPage + 1);
}
links.self = baseUrl + currentPage;
return links;
}
console.log(getPaginationLinks(0, 5));What error or output will this code produce?
function getPaginationLinks(currentPage, totalPages) {
const baseUrl = 'https://api.example.com/items?page=';
const links = {};
if (currentPage > 1) {
links.prev = baseUrl + (currentPage - 1);
}
if (currentPage < totalPages) {
links.next = baseUrl + (currentPage + 1);
}
links.self = baseUrl + currentPage;
return links;
}
console.log(getPaginationLinks(0, 5));Check the conditions for adding 'prev' and 'next' links when currentPage is 0.
Since currentPage is 0, the condition currentPage > 1 is false, so 'prev' is not added. currentPage < totalPages is true, so 'next' is added with page 1. 'self' is always added with page 0. No error occurs.
In Python, this code tries to access pagination links but raises a KeyError:
def get_links(page, total):
base = 'https://api.example.com/items?page='
links = {'self': base + str(page)}
if page > 1:
links['prev'] = base + str(page - 1)
if page < total:
links['next'] = base + str(page + 1)
print(links['prev'])Why does this raise a KeyError?
def get_links(page, total): base = 'https://api.example.com/items?page=' links = {'self': base + str(page)} if page > 1: links['prev'] = base + str(page - 1) if page < total: links['next'] = base + str(page + 1) print(links['prev'])
Check where the variable 'links' is defined and where it is accessed.
The variable 'links' is created inside the function get_links but the print statement tries to access it outside the function scope, so 'links' is not defined there, causing a NameError, not a KeyError. The question states KeyError, but the actual error is NameError.
Given a table items with 100 rows, this SQL query tries to get pagination info for page 3 with 10 items per page:
WITH total_count AS ( SELECT COUNT(*) AS total FROM items ), page_info AS ( SELECT 3 AS current_page, 10 AS per_page FROM dual ) SELECT current_page, per_page, total, CASE WHEN current_page > 1 THEN current_page - 1 ELSE NULL END AS prev_page, CASE WHEN current_page * per_page < total THEN current_page + 1 ELSE NULL END AS next_page FROM page_info, total_count;
What will be the values of prev_page and next_page in the result?
WITH total_count AS ( SELECT COUNT(*) AS total FROM items ), page_info AS ( SELECT 3 AS current_page, 10 AS per_page FROM dual ) SELECT current_page, per_page, total, CASE WHEN current_page > 1 THEN current_page - 1 ELSE NULL END AS prev_page, CASE WHEN current_page * per_page < total THEN current_page + 1 ELSE NULL END AS next_page FROM page_info, total_count;
Calculate if current_page * per_page is less than total rows to determine next_page.
Current page is 3, per page 10, so 3*10=30 which is less than total 100, so next_page is 4. Since current_page > 1, prev_page is 2.
In a REST API, a function generates pagination links for a list of 50 items with 10 items per page. The current page is 5 (the last page). The function adds 'prev' if current page > 1, 'next' if current page < total pages, and always adds 'self'.
How many links will be in the returned dictionary for page 5?
def pagination_links(current_page, total_items, per_page): total_pages = (total_items + per_page - 1) // per_page base = 'https://api.example.com/items?page=' links = {} if current_page > 1: links['prev'] = base + str(current_page - 1) if current_page < total_pages: links['next'] = base + str(current_page + 1) links['self'] = base + str(current_page) return links result = pagination_links(5, 50, 10) print(len(result))
Calculate total pages and check conditions for 'prev' and 'next' links on the last page.
Total pages = 50/10 = 5. Current page is 5, so 'prev' link is added (page 4), 'next' link is not added because current page is last. 'self' is always added. So total 2 links.