Classes, Encapsulation and Composition · Testing · 3 marks
public class Bag {
private final List<String> items = new ArrayList<>();
public void add(String item) { items.add(item); }
public int size() { return items.size(); }
public List<String> getItems() { return items; }
}
class BagTest {
@Test void addThenRead() {
Bag b = new Bag();
b.add("a");
assertEquals(List.of("a"), b.getItems());
}
}The test passes, yet getItems breaks the encapsulation of Bag. Write one more test method that fails because of it. [2]
In one sentence, why did addThenRead not notice? [1]
Modify the bag through the returned list and check size():
@Test void getItemsDoesNotExposeInternalList() {
Bag b = new Bag();
b.add("a");
b.getItems().add("x");
assertEquals(1, b.size());
}It fails with expected 1 but was 2. (Returning a copy, new ArrayList<>(items), would fix it: the test then changes only the copy.)
addThenRead only reads through the getter and never writes to what it returns, so a live reference and a copy give the same result.
Generics, Collections, Lambdas and Streams · Multiple choice · 1 mark
What does this code print?
List<Integer> nums = new ArrayList<>(List.of(10, 20, 30, 1));
nums.remove(1);
nums.remove(Integer.valueOf(1));
System.out.print(nums);[10, 20, 30]
[10, 30]
[10, 30, 1]
[10, 1]
Answer: (B)
(B) List has two overloads: remove(int index) and remove(Object o). The argument 1 is an int, so the first (an exact match, no boxing) removes the element at index 1, which is 20. The argument Integer.valueOf(1) is an object, so the second removes the first element equal to 1, which is the last one. (A) treats both calls as removing a value, (C) lets the second call do nothing, and (D) treats both as indexes.
Unit Testing with JUnit · Write code · 3 marks
JUnit 5 provides assertThrows. Write your own version that also hands the exception back to the test, so that the test can inspect its message.
/**
* Run action and return the exception it throws.
*
* @throws AssertionError if action finishes normally, or throws
* an exception that is not an instance of type
*/
public static <T extends Throwable> T expectThrows(
Class<T> type, Runnable action)public static <T extends Throwable> T expectThrows(
Class<T> type, Runnable action) {
try {
action.run();
} catch (Throwable t) {
if (type.isInstance(t)) {
return type.cast(t);
}
throw new AssertionError("wrong exception: " + t, t);
}
throw new AssertionError("nothing was thrown");
}The last throw must be outside the try: inside it, the AssertionError would be caught by the catch (Throwable t) clause and (for type = Throwable.class) returned as if it were the expected exception.
Design Patterns · Short answer · 2 marks
Subclasses of Report must be able to change what is printed, but never the order of the steps.
public abstract class Report {
public void print() {
System.out.println(header());
System.out.println(body());
}
protected String header() { return "REPORT"; }
protected abstract String body();
}Which method should be declared final, and why? [1]
Why is body abstract while header has an implementation? [1]
print: it is the template method that fixes the sequence of steps. If a subclass could override it, it could reorder or skip the steps, defeating the point of the pattern.
body has no sensible default, so every subclass must supply it (the compiler enforces that); header is a hook with a reasonable default that a subclass may override but does not have to.
Version Control with Git · Design · 3 marks
Release 1.0 is ready on main.
Write the command that marks the current commit with an annotated tag v1.0 and the message “Release 1.0”. [1]
After a plain git push, the tag does not appear on origin. Write the command that publishes it. [1]
The team continues to commit on main. What happens to the commit that v1.0 points to, and how does that differ from a branch? One sentence. [1]
git tag -a v1.0 -m "Release 1.0"
git push origin v1.0 (or git push --tags to send all tags).
Nothing: a tag always names the same commit, so v1.0 keeps identifying exactly what was released, while a branch such as main moves forward with each new commit.
Software Construction · University of British Columbia (UBC, UBC Vancouver) · also written CPSC210
Make a free, printable CPSC 210 practice exam: choose the topics, the length and the difficulty above, and get an exam-style PDF with a full answer key. Each paper draws on 320 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the UBC library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh CPSC 210 practice exams here on the same topics, with full answer keys, as many as you need. The questions here are original, not copies of past exams.
questionbank.dev is independent and not affiliated with University of British Columbia. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.