Interfaces, Generics and Dynamic Dispatch · Short answer · 2 marks
In a generic class ArrayStack<T>, the constructor contains the line
items = new T[capacity];which does not compile. In two or three sentences, explain why, and write the one line that is normally used instead.
Generics are erased: at run time there is no T, but creating an array requires the JVM to know the element type. Instead, the constructor creates an Object[] and casts it (the cast is unchecked, so the line carries @SuppressWarnings("unchecked")):
items = (T[]) new Object[capacity];ArrayLists, Linked Lists and Deques · Multiple choice · 1 mark
The variable it is a fresh Iterator<Integer> over the list [1, 2, 3, 4]. Which sequence of calls throws an IllegalStateException?
it.next(); it.next(); it.remove();
it.next(); it.remove(); it.remove();
it.hasNext(); it.next(); it.remove();
it.next(); it.remove(); it.next(); it.remove();
Answer: (B)
(B). remove() deletes the element returned by the most recent next(), and it may be called only once per next(). The second consecutive remove() has no such element, so it throws. The other sequences call next() before every remove() (hasNext() does not matter).
Stacks, Queues and Their Uses · Multiple choice · 1 mark
An array-backed stack starts with capacity 10. Whenever it is full, push replaces the array by a new array that is exactly 10 larger and copies the old items. Counting one step per item copied, what is the cost of a push averaged over a sequence of pushes (no pops)?
: the pushes copy about items in total
amortized: the array is resized only once every 10 pushes
: the number of resizes grows logarithmically
amortized: any growth step of at least 2 is enough
Answer: (A)
(A). There are about resizes, and the -th one copies about items, so the total is about copies and the average cost per push is . Doubling the capacity makes the resize sizes geometric (total under ), which is what gives amortized ; a fixed increment does not.
Recursion and Backtracking · Predict the output · 2 marks
What does the program print? Write each line of output on its own line.
public class Main {
static void walk(int lo, int hi) {
if (lo > hi) {
return;
}
int mid = (lo + hi) / 2;
System.out.println(mid);
walk(lo, mid - 1);
walk(mid + 1, hi);
}
public static void main(String[] args) {
walk(1, 6);
}
}3
1
2
5
4
6walk(1, 6) prints 3, then handles the left range 1..2 completely (it prints 1, then 2) before it starts the right range 4..6 (it prints 5, then 4, then 6).
Sorting Algorithms · Short answer · 2 marks
Every item of an array of items is at most positions away from the position it will have in the sorted array. Give the running time of insertion sort on such an array in terms of and , and justify it briefly.
(for example when is a constant). The inner loop shifts an item once for every larger item to its left, so the total work is plus the number of inversions. Two items that form an inversion must both be within places of their final positions, so their current positions differ by less than ; each item therefore forms an inversion with fewer than items on its left, and there are shifts in all.
Computer Science Fundamentals II · Western University (Western, Western U, UWO) · also written COMPSCI1027, COMPSCI 1027
Make a free, printable COMPSCI 1027A/B 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 300 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the Western library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh COMPSCI 1027A/B 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 Western University. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.