Answer key · 12 questions

Python pagination and deduplication Exercises With Answers

All 12 questions for Python pagination and deduplication in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python pagination and deduplication, with the exact answer and a short explanation for every question.

The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.

01 · Multiple choice

Why keep a visited set during a crawl?

  1. To sort the pages alphabetically
  2. To avoid cycles and repeated pagescorrect
  3. To store the HTML
  4. To change the User-Agent

Answer: To avoid cycles and repeated pages

Why: A visited set remembers URLs that were already processed.

02 · Multiple choice

Which deque method removes the leftmost item?

  1. pop()
  2. removeleft()
  3. shift()
  4. popleft()correct

Answer: popleft()

Why: popleft removes from the front, which gives FIFO order for a queue.

03 · Multiple choice

Why set a maximum page count?

  1. To bound the work and stop runaway crawlscorrect
  2. To make URLs shorter
  3. To remove query strings
  4. To decode HTML entities

Answer: To bound the work and stop runaway crawls

Why: A page budget is a simple stop condition even when links keep appearing.

04 · Multiple choice

What does this code print?

from collections import deque
pages = {"/1": ["/2", "/a"], "/2": ["/b"]}
queue = deque(["/1"])
seen = []
while queue and len(seen) < 3:
    url = queue.popleft()
    if url in seen:
        continue
    seen.append(url)
    queue.extend(pages.get(url, []))
print(seen)
  1. ['/1', '/a', '/2']
  2. ['/1', '/2', '/a']correct
  3. ['/1', '/2', '/a', '/b']
  4. ['/2', '/1', '/a']

Answer: ['/1', '/2', '/a']

Why: The breadth-first queue visits /1, then its links /2 and /a, stopping at the budget of three.

05 · Fill in the blank

Complete the queue type.

from collections import deque
queue = ____(["/1", "/2"])
print(queue.popleft())

Answer: deque

Why: deque supports efficient appends and popleft operations.

06 · Fill in the blank

Complete the method that removes the next URL.

from collections import deque
queue = deque(["/1", "/2"])
print(queue.____())

Answer: popleft

Why: popleft removes the leftmost item from a deque.

07 · Fill in the blank

Complete the set insertion call.

seen = set()
url = "/a"
seen.____(url)
print(len(seen))

Answer: add

Why: Set.add records a URL as visited.

08 · Fill in the blank

Complete the membership check.

seen = {"/a"}
url = "/a"
print(url in ____)

Answer: seen

Why: Checking membership in the visited set prevents a second visit.

09 · Predict output

What does the program output?

links = ["/a", "/b", "/a"]
print(list(dict.fromkeys(links)))

Answer: ['/a', '/b']

Why: dict.fromkeys preserves first-seen order while removing duplicates.

10 · Predict output

What appears on the screen?

pages = {"/1": ["/2"], "/2": ["/3"], "/3": []}
queue = ["/1"]
seen = []
while queue:
    url = queue.pop(0)
    if url in seen:
        continue
    seen.append(url)
    queue.extend(pages.get(url, []))
print(seen)

Answer: ['/1', '/2', '/3']

Why: The queue visits each page once in breadth-first order.

11 · Predict output

What is printed?

seen = set()
for url in ["/a", "/a", "/b"]:
    seen.add(url)
print(len(seen))

Answer: 2

Why: The set contains two unique URLs.

12 · Predict output

What is the output?

from urllib.parse import urlsplit, urlunsplit
parts = urlsplit("https://example.com/guide.html#top")
print(urlunsplit((parts.scheme, parts.netloc, parts.path, parts.query, "")))

Answer: https://example.com/guide.html

Why: Replacing the fragment with an empty string removes #top.

Try the questions first

Open the practice page for Python pagination and deduplication and answer the same 12 questions with instant feedback.

Practice this lesson