~15 minutes, 4 questions. Intended for ELMS online quiz.
You're writing a script to clean URLs for privacy. What is the output of the following code?
urls = [
"https://shop.com/item?utm_source=twitter",
"http://news.org/article?ref=homepage",
"https://blog.io/post?utm_campaign=sale",
"https://docs.dev/page"
]
cleaned = []
tracker_count = 0
for url in urls:
url = url.replace("http://", "https://")
if "?" in url:
base, param = url.split("?")
if param.startswith("utm"):
cleaned.append(base)
tracker_count += 1
else:
cleaned.append(url)
else:
cleaned.append(url)
print(len(cleaned))
print(tracker_count)
print(cleaned[1])- A)
4then2thenhttps://news.org/article?ref=homepage- Note: Correct! Put in rationale from below.
- B)
2then2thenhttps://blog.io/post- Note: we're appending all urls to cleaned (this is a transform pattern), so len(cleaned) is 4 (same as input). blog.io/post is the 3rd item in cleaned (cleaned[2])
- C)
4then2thenhttp://news.org/article?ref=homepage- Note: Correct number of cleaned and tracked, and the correct url, but it should be https instead of http since we're doing
url.replace("http://", "https://")
- Note: Correct number of cleaned and tracked, and the correct url, but it should be https instead of http since we're doing
- D)
4then3thenhttps://news.org/article- Note: Correct number of cleaned, but not tracked (only two
utm_params); so news.org url (correct position, cleaned[1]) should have the?ref=homepageparam since it's not a tracker param
- Note: Correct number of cleaned, but not tracked (only two
- E)
3then2thenhttps://news.org/article?ref=homepage- Note: Correct number of tracked, and correct url, but not
len(cleaned)
- Note: Correct number of tracked, and correct url, but not
Correct answer: A
Rationale: Walk through each URL:
"https://shop.com/item?utm_source=twitter"— has?, param starts withutm→ append base"https://shop.com/item", tracker_count = 1"http://news.org/article?ref=homepage"— first replaced tohttps://..., has?, param isref=homepagewhich does NOT start withutm→ append the full url"https://news.org/article?ref=homepage""https://blog.io/post?utm_campaign=sale"— has?, param starts withutm→ append base"https://blog.io/post", tracker_count = 2"https://docs.dev/page"— no?→ append as-is
All 4 URLs end up in cleaned (len = 4). Two had trackers (tracker_count = 2). cleaned[1] is the news URL with https:// and ?ref=homepage preserved.
Key distractors:
- B tests whether students think non-tracker URLs are excluded
- C tests whether students miss the
http→httpsreplacement - D tests whether students think
ref=homepagestarts withutm - E tests whether students think URLs without
?are excluded
For each of the following tasks, select whether a for loop or a while loop is the best (most suitable, idiomatic) choice.
(2a) Process every item in a list of 100 student grades and count how many are above 90.
- A)
forloop - B)
whileloop- Note: We know the exact collection to iterate through (the list of grades) and we need to visit every item. This is the textbook use case for a
forloop, now awhile: you could solve withwhile, but it's unnecessarily clunkier.
- Note: We know the exact collection to iterate through (the list of grades) and we need to visit every item. This is the textbook use case for a
Correct answer: A
Rationale: We know the exact collection to iterate through (the list of grades) and we need to visit every item. This is the textbook use case for a for loop.
(2b) Keep asking the user for a password until they enter the correct one.
- A)
forloop- Note: We don't know in advance how many attempts it will take. We need to keep going until a condition is met (correct password entered). This requires a
whileloop.
- Note: We don't know in advance how many attempts it will take. We need to keep going until a condition is met (correct password entered). This requires a
- B)
whileloop
Correct answer: B
Rationale: We don't know in advance how many attempts it will take. We need to keep going until a condition is met (correct password entered). This requires a while loop.
(2c) Search through a list of names and stop as soon as you find "Joel".
- A)
forloop (withbreak)- Note: You could use a
forloop withbreakto stop early, OR awhileloop with an index that advances until the name is found or you reach the end. Both are reasonable approaches. Thefor+breakapproach is slightly simpler since you don't need to manage the index yourself.
- Note: You could use a
- B)
whileloop- Note: You could use a
forloop withbreakto stop early, OR awhileloop with an index that advances until the name is found or you reach the end. Both are reasonable approaches. Thefor+breakapproach is slightly simpler since you don't need to manage the index yourself.
- Note: You could use a
- C) Either would work well
Correct answer: C
Rationale: You could use a for loop with break to stop early, or a while loop with an index that advances until the name is found or you reach the end. Both are reasonable approaches. The for + break approach is slightly simpler since you don't need to manage the index yourself.
Consider this code that filters a list based on a condition:
prices = [12.50, 3.99, 25.00, 8.75, 45.00, 2.50]
expensive = []
for price in prices:
if price > 10:
expensive.append(price)Which of the following programs uses the SAME computational pattern?
- A)
words = ["hello", "hi", "hey", "howdy"]
total = 0
for word in words:
total += len(word)Note: The original code uses the filter pattern: iterate through a list, check a condition, and collect only the items that pass into a new list. A is an accumulator pattern (summing values, no conditional collection), not a filter.
- B)
temps = [72, 85, 68, 91, 77, 95, 60]
hot_days = []
for temp in temps:
if temp > 90:
hot_days.append(temp)Note: Correct! B is a filter (collect temps > 90) — same pattern, different data and condition.
- C)
names = ["Joel", "Sarah", "John"]
upper_names = []
for name in names:
upper_names.append(name.upper())Note: The original code uses the filter pattern: iterate through a list, check a condition, and collect only the items that pass into a new list. D is a transform/map pattern (doubling every item with no filtering), not a filter.
- D)
nums = [1, 2, 3, 4, 5]
doubled = []
for num in nums:
doubled.append(num * 2)Note: The original code uses the filter pattern: iterate through a list, check a condition, and collect only the items that pass into a new list. C is a transform/map pattern (applying a transformation to every item with no filtering), not a filter.
Correct answer: B
Rationale: The original code uses the filter pattern: iterate through a list, check a condition, and collect only the items that pass into a new list.
- B is a filter (collect temps > 90) — same pattern, different data and condition.
- A is an accumulator pattern (summing values, no conditional collection).
- C is a transform/map pattern (applying a transformation to every item with no filtering).
- D is also a transform/map pattern (doubling every item with no filtering).
A student wrote the following program to clean up a list of course codes by converting them to uppercase. But it doesn't work — the output is still the original list with mixed casing. Which line has the bug, and why?
codes = ["inst126", "CMSC132", "Inst201", "math140"]
for i in range(len(codes)):
codes[i].upper()
print(codes)- A) The
range(len(codes))should just berange(codes)— you can't uselen()withrange().- Note:
range(len(codes))is perfectly valid syntax! The issue is that strings are immutable —.upper()returns a new string but does not modify the original. We need to reassign:codes[i] = codes[i].upper().
- Note:
- B) Line A calls
.upper()but doesn't save the result. It should becodes[i] = codes[i].upper()because strings are immutable. - C) The
forloop should usefor code in codes:instead of index-based iteration — that's why the.upper()doesn't work.- Note: index-based iteration is fine! The issue is that strings are immutable —
.upper()returns a new string but does not modify the original. We need to reassign:codes[i] = codes[i].upper(). Plus, afor code in codesloop would have the same problem sincecode = code.upper()wouldn't modify the list either!
- Note: index-based iteration is fine! The issue is that strings are immutable —
- D)
.upper()doesn't work on strings that are already partially uppercase like"CMSC132".- Note:
.upper()works on any string regardless of existing casing
- Note:
Correct answer: B
Rationale: Strings are immutable — .upper() returns a new string but does not modify the original. The student needs to reassign: codes[i] = codes[i].upper().
Key distractors:
- A is wrong because
range(len(codes))is perfectly valid syntax - C is wrong because index-based iteration is fine; the issue is immutability, not the loop style (and actually, a
for code in codesloop would have the same problem sincecode = code.upper()wouldn't modify the list either!) - D is wrong because
.upper()works on any string regardless of existing casing
Concepts tested across questions:
| Concept | Q1 | Q2 | Q3 | Q4 |
|---|---|---|---|---|
| for loops | x | x | x | x |
| while loops | x | |||
| String methods (.replace, .startswith, .split) | x | x | ||
in operator (substring check) |
x | |||
| List building (.append) | x | x | ||
| Conditionals inside loops | x | x | ||
| String immutability | x | |||
| Filter pattern | x | |||
| Transform pattern | x | |||
| Accumulator pattern | x | x | ||
| for vs while reasoning | x |
Bloom's taxonomy levels:
- Q1: Apply (trace execution)
- Q2: Analyze (choose appropriate construct for a scenario)
- Q3: Analyze (recognize structural similarity across different problems)
- Q4: Evaluate (diagnose a bug and identify the root cause)
ELMS implementation notes:
- Q1: Single-answer multiple choice
- Q2: Three separate sub-questions, each single-answer MC (could be grouped as one ELMS question with parts, or three quick standalone questions)
- Q3: Single-answer multiple choice
- Q4: Single-answer multiple choice