Sample EECS 2030 questions
  1. 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.

    Show solution
    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 !=.

  2. 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);
        }
    }
    Show solution
    3
    1
    2
    5
    4
    6

    walk(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).

  3. Sorting Algorithms · Multiple choice · 1 mark

    This quicksort uses the last item of the range as the pivot. Four arrays of the same length nn 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);
    }
    1. The items already in ascending order

    2. The items in descending order

    3. All nn items equal

    4. The items in a scrambled order, with no pattern

    Show solution

    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 (n−1)+(n−2)+⋯+1=Θ(n2)(n-1)+(n-2)+\cdots+1 = \Theta(n^2) comparisons. A scrambled input gives reasonably balanced splits on average and about nlog⁡nn \log n comparisons.

  4. 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());
        }
    }
    Show solution
    x
    null
    null
    empty
    0

    pop 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.

  5. Binary Trees and Binary Search Trees · Short answer · 2 marks

    A binary tree built from TreeNode objects has n≥1n \ge 1 nodes. How many of its 2n2n fields left and right are null? Give the number and a short argument.

    Show solution

    n+1n + 1. Every node except the root is the child of exactly one node, so exactly n−1n - 1 of the 2n2n child fields are non-null, and 2n−(n−1)=n+12n - (n - 1) = n + 1 of them are null.

About EECS 2030 practice exams

EECS 2030 practice exams

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.

Looking for EECS 2030 past exams?

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.

Topics covered

  • Interfaces, Generics and Dynamic Dispatch · 50 questions
  • Recursion and Backtracking · 50 questions
  • Sorting Algorithms · 50 questions
  • Stacks, Queues and Their Uses · 50 questions
  • ArrayLists, Linked Lists and Deques · 50 questions
  • Binary Trees and Binary Search Trees · 50 questions

More York courses

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.