Bird
Raised Fist0

You have this code snippet for pagination parameters:

medium📝 Debug Q14 of Q15
Rest API - Pagination Patterns
You have this code snippet for pagination parameters:
page = int(request.GET.get('page', 1))
limit = int(request.GET.get('limit', 10))
start = (page - 1) * limit
end = page * limit
items = data[start:end]

What is the error if page is 0?
AIt causes negative start index, returning wrong items
BIt returns the last page instead of first
CIt raises a syntax error
DIt ignores the limit parameter
Step-by-Step Solution
Solution:
  1. Step 1: Calculate start index with page=0

    start = (0 - 1) * limit = -1 * limit = negative number.
  2. Step 2: Understand slicing with negative start

    Negative start index in slicing returns items from the end, causing wrong data to be returned.
  3. Final Answer:

    It causes negative start index, returning wrong items -> Option A
  4. Quick Check:

    page=0 causes negative start index [OK]
Quick Trick: Page must be >= 1 to avoid negative start index [OK]
Common Mistakes:
MISTAKES
  • Assuming page=0 is valid
  • Expecting syntax error instead of logic error
  • Ignoring negative slicing effects

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes