Sample ITI 1121 questions
  1. Interfaces, Generics and Dynamic Dispatch · Multiple choice · 2 marks

    Consider the following declarations, where Dog is a subclass of Animal.

    class Animal { }
    class Dog extends Animal { }
    // in main:
    Animal a = new Dog();

    Select all of the declarations below that compile (assume java.util.* is imported).

    1. Dog d = a;

    2. Dog d = (Dog) a;

    3. List<Animal> xs = new ArrayList<Dog>();

    4. Animal[] zs = new Dog[3];

    5. Dog e = new Animal();

    Show solution

    Answer: (B, D)

    Dog d = (Dog) a; compiles (and succeeds, because a really is a Dog); Animal[] zs = new Dog[3]; compiles because arrays are covariant: a Dog[] is an Animal[]. The other three do not compile. Dog d = a; and Dog e = new Animal(); need a cast or a different object, since a supertype cannot be assigned to a subtype variable without a cast. List<Animal> xs = new ArrayList<Dog>(); fails because generic types are invariant: ArrayList<Dog> is not a List<Animal> even though Dog is an Animal.

  2. ArrayLists, Linked Lists and Deques · Predict the output · 3 marks

    What does the following code print? Write each line of output on its own line.

    List<Integer> xs = new ArrayList<>(List.of(3, 8, 5, 6, 7));
    Iterator<Integer> it = xs.iterator();
    int seen = 0;
    while (it.hasNext()) {
        int x = it.next();
        seen += x;
        if (x % 2 == 0 || seen > 20) {
            it.remove();
        }
        System.out.println(x + " " + xs.size());
    }
    System.out.println(xs);
    Show solution
    3 5
    8 4
    5 4
    6 3
    7 2
    [3, 5]

    The running total seen is 3, 11, 16, 22, 29. The elements 8 and 6 are removed because they are even, and 7 is removed because seen has passed 20; the iterator’s remove() is legal each time because next() was just called.

  3. Stacks, Queues and Their Uses · Predict the output · 2 marks

    What does the program print? Assume java.util.* is imported.

    public class Main {
        public static void main(String[] args) {
            Deque<Integer> q = new ArrayDeque<>();
            for (int i = 1; i <= 5; i++) {
                q.offer(i);
            }
            while (q.size() > 1) {
                q.offer(q.poll());
                System.out.print(q.poll() + " ");
            }
            System.out.println(q.peek());
        }
    }
    Show solution
    2 4 1 5 3

    Each pass moves the front item to the back (it survives) and then removes the next one. The queue goes 1 2 3 4 5, then 3 4 5 1 (printing 2), then 5 1 3 (printing 4), then 3 5 (printing 1), then 3 (printing 5), and the last survivor, 3, is printed by println.

  4. Recursion and Backtracking · Write code · 3 marks

    Complete the method below using recursion and no loop. It must discard half of the range at each call.

    /** Return an index i with lo <= i <= hi and a[i] == target,
     *  or -1 if there is none.
     *  Precondition: a[lo..hi] is sorted in increasing order.
     *  Example: for a = {1, 3, 5, 7}, search(a, 5, 0, 3) returns 2
     *  and search(a, 4, 0, 3) returns -1. */
    static int search(int[] a, int target, int lo, int hi) {
    Show solution
    static int search(int[] a, int target, int lo, int hi) {
        if (lo > hi) {
            return -1;
        }
        int mid = lo + (hi - lo) / 2;
        if (a[mid] == target) {
            return mid;
        }
        if (a[mid] < target) {
            return search(a, target, mid + 1, hi);
        }
        return search(a, target, lo, mid - 1);
    }

    An empty range (lo > hi) is the base case. Both recursive calls exclude mid, so the range shrinks by at least one element at each call and the recursion ends. Using mid itself as a bound would loop forever on a range of one or two elements.

  5. Binary Trees and Binary Search Trees · Write code · 3 marks

    Complete the method below. Change the links of the existing nodes; do not create new nodes.

    /** Remove every leaf of the binary tree rooted at root and return
     *  the root of the resulting tree. A tree that is a single node
     *  becomes empty. Nodes that become leaves only because their
     *  children were removed are kept.
     *  Example: for the tree with root 1, left child 2 (which has leaf
     *  children 4 and 5) and right child 3, returns the tree with root 1
     *  and left child 2 only. */
    static TreeNode removeLeaves(TreeNode root) {
    Show solution
    static TreeNode removeLeaves(TreeNode root) {
        if (root == null) return null;
        if (root.left == null && root.right == null) return null;
        root.left = removeLeaves(root.left);
        root.right = removeLeaves(root.right);
        return root;
    }

    Each call returns the root of the pruned subtree: null for a leaf, otherwise the same node with its two links replaced by the pruned subtrees. The leaf test is done on the node before the recursive calls, so a node that becomes a leaf by pruning is not removed.

About ITI 1121 practice exams

ITI 1121 practice exams

Introduction to Computing II · University of Ottawa (uOttawa, U of O, UOttawa) · also written ITI1121

Make a free, printable ITI 1121 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 250 original questions and skips the ones you have already seen.

Looking for ITI 1121 past exams?

Start with the past exams and tests your instructor or the uOttawa library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh ITI 1121 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

More uOttawa courses

questionbank.dev is independent and not affiliated with University of Ottawa. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.