Interfaces, Generics and Dynamic Dispatch · Write code · 3 marks
The class Evens lets a client loop over the elements of an array that have an even index.
class Evens implements Iterable<Integer> {
private final int[] data;
Evens(int[] data) { this.data = data; }
public Iterator<Integer> iterator() { return new EvensIterator(data); }
}Write the class EvensIterator, which implements Iterator<Integer> and returns data[0], data[2], data[4], … in this order (for {5, 6, 7, 8, 9} it returns 5, 7 and 9). Its next method throws NoSuchElementException when there is nothing left. The constructor receives the array.
class EvensIterator implements Iterator<Integer> {
private final int[] data;
private int index = 0;
EvensIterator(int[] data) {
this.data = data;
}
public boolean hasNext() {
return index < data.length;
}
public Integer next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
int value = data[index];
index += 2;
return value;
}
}The iterator keeps the position of the next element to return. hasNext must stay correct when index has jumped past the end (for an array of even length the last jump leaves index == data.length + 1), so the test is < and 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 · Multiple choice · 1 mark
This quicksort uses the last item of the range as the pivot. Four arrays of the same length are sorted with it. For which input does it make the fewest comparisons?
static void quickSort(int[] a, int lo, int hi) {
if (lo >= hi) return;
int pivot = a[hi];
int i = lo;
for (int j = lo; j < hi; j++) {
if (a[j] < pivot) { swap(a, i, j); i++; }
}
swap(a, i, hi);
quickSort(a, lo, i - 1);
quickSort(a, i + 1, hi);
}The items already in ascending order
The items in descending order
All items equal
The items in a scrambled order, with no pattern
Answer: (D)
(D). In the other three inputs the pivot is always an extreme of its range: the maximum for ascending input, the minimum for descending input, and (because equal items are not < the pivot and so stay in the right part) the smallest value for equal items. Every call then splits off just one item, which costs comparisons. A scrambled input gives reasonably balanced splits on average and about comparisons.
Stacks, Queues and Their Uses · Predict the output · 2 marks
What does the program print? Write each line of output on its own line. Assume java.util.* is imported.
public class Main {
public static void main(String[] args) {
Deque<String> d = new ArrayDeque<>();
d.push("x");
System.out.println(d.pop());
System.out.println(d.poll());
System.out.println(d.peek());
try {
System.out.println(d.pop());
} catch (NoSuchElementException e) {
System.out.println("empty");
}
System.out.println(d.size());
}
}x
null
null
empty
0pop returns the only item. On the now empty deque, poll and peek return null (and println prints the word null), while pop throws NoSuchElementException, which the catch block turns into the line empty.
Binary Trees and Binary Search Trees · Short answer · 2 marks
A binary tree built from TreeNode objects has nodes. How many of its fields left and right are null? Give the number and a short argument.
. Every node except the root is the child of exactly one node, so exactly of the child fields are non-null, and of them are null.
Advanced Object Oriented Programming · York University (York, YorkU, York U) · also written EECS2030
Make a free, printable EECS 2030 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 York library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh EECS 2030 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 York University. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.