Purpose: Self-check on lists, 2-D lists, dictionaries, strings, and classes/objects. Instant feedback. Python 3.
Score: 0 / 12
Question 1 — List indexing
Given a = [10, 20, 30, 40, 50], what does a[-2] evaluate to?
Solution:
Negative indices count from the end: -1 is last, -2 is second-last. Answer: 40.
Question 2 — Slicing
Given a = [0, 1, 2, 3, 4, 5, 6], what does a[2:5] produce?
Solution:
Slice [start:stop] — start is inclusive, stop is exclusive. Indices 2, 3, 4 give [2, 3, 4].
Question 3 — List mutation
After this code, what is the value of x?
x = [1, 2, 3]
y = x
y.append(4)
print(x)
Solution:
[1, 2, 3, 4]. y = x binds y to the same list object — it does NOT make a copy. Mutating through y changes the list x also references. Use y = x.copy() or y = list(x) for an independent copy.
Question 4 — 2-D list traversal
What is the sum of all elements?
grid = [[1, 2], [3, 4], [5, 6]]
total = 0
for row in grid:
for cell in row:
total += cell
print(total)
Solution:
1+2+3+4+5+6 = 21.
Question 5 — Dictionary access
Given d = {"a": 1, "b": 2, "c": 3}, what does d.get("z", 0) return?
Solution:
.get(key, default) returns the default if the key is missing. Answer: 0. Compare with d["z"], which would raise KeyError.
Question 6 — Dictionary iteration
What is printed?
votes = {"yes": 12, "no": 5, "abstain": 3}
total = 0
for v in votes.values():
total += v
print(total)
Solution:
12 + 5 + 3 = 20.
Question 7 — String slicing
Given s = "Computer Science", what is s[:8]?
Solution:
s[:8] = first 8 characters = "Computer".
Question 8 — String methods
What does this print?
x = " hello world "
print(x.strip().split())
Solution:
.strip() removes outer whitespace; .split() with no argument splits on whitespace. Result: ['hello', 'world'].
Question 9 — List of dicts (records)
Given:
students = [
{"name": "Ada", "grade": 88},
{"name": "Bob", "grade": 75},
{"name": "Cay", "grade": 92}
]
total = 0
for s in students:
total += s["grade"]
print(total / len(students))
What is printed?
Solution:
Sum = 88+75+92 = 255. Average = 255 / 3 = 85.0.
Question 10 — Class basics
Given:
class Box:
def __init__(self, w, h):
self.w = w
self.h = h
def area(self):
return self.w * self.h
b = Box(4, 5)
print(b.area())