📝 Unit 2: Data Structures — Unit Test

Assessment OF Learning — Summative
✅ Graded — Counts Toward 70% Term Mark
⏱️ Duration: 75 minutes  |  Total: /60 marks
Show all work. Code answers must be syntactically valid Python 3. No internet, no AI tools.
K/U
/15
Thinking
/15
Comm.
/15
Applic.
/15
Part A: Knowledge & Understanding [15 marks]
1. [2 marks]
Given nums = [3, 1, 4, 1, 5, 9, 2, 6], write the value of:
  1. nums[3]
  2. nums[-1]
  3. nums[2:6]
  4. len(nums)
2. [3 marks]
Trace this code and report the final value of d.
d = {} words = ["red", "blue", "red", "red", "blue", "green"] for w in words: d[w] = d.get(w, 0) + 1 print(d)
3. [2 marks]
Write a single line of Python that creates a 2-D list (3 rows × 4 columns) initialized with all zeros.
4. [2 marks]
What is the output?
s = "abracadabra" print(s.count("a"), s.replace("a", "*"))
5. [3 marks]
Given:
class Point: def __init__(self, x, y): self.x = x self.y = y def distance_from_origin(self): return (self.x**2 + self.y**2) ** 0.5 p = Point(3, 4) print(p.distance_from_origin())
What is printed?
6. [3 marks]
For each scenario, choose the most appropriate data structure (list, dictionary, 2-D list, or class) and briefly justify.
  1. Storing a chess board.
  2. Looking up a word's definition.
  3. Modeling a "Vehicle" with attributes (make, model, year) and methods (start, stop).
Part B: Thinking & Investigation [15 marks]
7. [5 marks]
Find and fix the bugs. The function below should remove duplicates from a list and return the result, preserving order. There are two bugs.
def deduplicate(items): seen = [] result = [] for x in items: if x not in seen: seen.append(x) result.append(x) return seen
8. [5 marks]
Trace this 2-D list code. Show the final state of grid.
grid = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] for i in range(3): for j in range(3): if i == j: grid[i][j] = 1 elif (i + j) == 2: grid[i][j] = -1 print(grid)
9. [5 marks]
Design a class BankAccount with:
  • An __init__ that takes the owner name and an optional starting balance (default 0).
  • A deposit(amount) method.
  • A withdraw(amount) method that does not allow the balance to go below 0; if the withdrawal would overdraw, raise a ValueError.
  • A __str__ that returns "<owner>: $<balance>".
Part C: Communication [15 marks]
10. [5 marks]
Explain in your own words: when would you choose a dictionary over a list? Provide one specific example where each is best suited and explain the trade-offs.
11. [5 marks]
A peer claims: "I copied the list with b = a, so when I sort b the original is unaffected." Explain why this is wrong, what actually happens, and provide the correct way to make an independent copy.
12. [5 marks]
Add docstrings and inline comments to this class so a future reader can understand each method's purpose and constraints.
class Stack: def __init__(self): self.items = [] def push(self, x): self.items.append(x) def pop(self): return self.items.pop() def peek(self): return self.items[-1] def empty(self): return len(self.items) == 0
Part D: Application [15 marks]
13. [5 marks]
Write a function most_common(words) that takes a list of strings and returns the string that appears most often. If there is a tie, return any one of the tied words. Test it with most_common(["red","blue","red","green","red","blue"]) — show the result.
14. [5 marks]
A CSV file marks.csv contains lines of the form name,mark, e.g.:
Ada,88 Bob,75 Cay,92
Write a complete program that reads the file and prints the name of the student with the highest mark, plus the class average to 1 decimal place.
15. [5 marks]
Design a 2-D list representation for a 4×4 game board where each cell can be empty (" "), "X", or "O". Write a function print_board(board) that displays the board nicely, with column and row labels (1-4 / A-D), and a function place(board, col_letter, row_num, mark) that places a mark and returns True, or returns False if the cell is taken.

Evaluation Rubric

LevelDescription%
4Thorough, insightful; correct, idiomatic, well-commented code80–100%
3Considerable effectiveness (provincial standard) — mostly correct with minor issues70–79%
2Some effectiveness — partial credit; logic largely sound but contains errors60–69%
1Limited effectiveness50–59%
RInsufficientBelow 50%