Sample COMPSCI 1027A/B questions
  1. 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.

    Show solution

    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];
  2. 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?

    1. it.next(); it.next(); it.remove();

    2. it.next(); it.remove(); it.remove();

    3. it.hasNext(); it.next(); it.remove();

    4. it.next(); it.remove(); it.next(); it.remove();

    Show solution

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

  3. 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 nn pushes (no pops)?

    1. Θ(n)\Theta(n): the nn pushes copy about n2/20n^2/20 items in total

    2. O(1)O(1) amortized: the array is resized only once every 10 pushes

    3. Θ(log⁡n)\Theta(\log n): the number of resizes grows logarithmically

    4. O(1)O(1) amortized: any growth step of at least 2 is enough

    Show solution

    Answer: (A)

    (A). There are about n/10n/10 resizes, and the kk-th one copies about 10k10k items, so the total is about 10 (n/10)2/2=n2/2010\,(n/10)^2/2 = n^2/20 copies and the average cost per push is Θ(n)\Theta(n). Doubling the capacity makes the resize sizes geometric (total under 2n2n), which is what gives amortized O(1)O(1); a fixed increment does not.

  4. 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).

  5. Sorting Algorithms · Short answer · 2 marks

    Every item of an array of nn items is at most kk 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 nn and kk, and justify it briefly.

    Show solution

    O(nk)O(nk) (for example O(n)O(n) when kk is a constant). The inner loop shifts an item once for every larger item to its left, so the total work is nn plus the number of inversions. Two items that form an inversion must both be within kk places of their final positions, so their current positions differ by less than 2k2k; each item therefore forms an inversion with fewer than 2k2k items on its left, and there are O(nk)O(nk) shifts in all.

About COMPSCI 1027A/B practice exams

COMPSCI 1027A/B practice exams

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.

Looking for COMPSCI 1027A/B past exams?

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.

Topics covered

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

More Western courses

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.