CS 112
Fall 2026
  • Home
  • Syllabus
  • Staff
  • Office Hours
  • Labs
  • Problem Sets
  • Resources
  • Collaboration
  • Lectures
  • Piazza
  • Gradescope

Quiz 2 Practice Problems

Solutions will be posted under Practice Problem Solutions on Blackboard as we get closer to the exam.

These problems are not comprehensive, so make sure to review all of the relevant materials. We strongly recommend reviewing problems and methods from Part II of the relevant problem sets in addition to these practice problems.

Questions 1 and 2 involve linked lists of integers that are constructed from nodes whose class definition begins as follows:

public class Node {
    private int val;
    private Node next;

    public Node() {
        this.val = 0;
        this.next = null;
    }
    ...
}
  1. You want to add a method to the Node class that takes two parameters:

    • a reference to the first node in a linked list (or null if the list is empty)

    • an integer x

    The method should insert a node containing x at the front of the list, and it should return a reference to the new front of the list.

    Which of these methods does this correctly?

    option I:

    public static Node insertFront(Node first, int x) {
        first = new Node();
        first.val = x;
        first.next = first;
        return first;
    }
    

    option II:

    public static Node insertFront(Node first, int x) {
        Node n = new Node();
        n.val = x;
        n.next = first;
        return n;
    }
    

    option III:

    public static Node insertFront(Node first, int x) {
        Node n = new Node();
        first = n;
        n.val = x;
        n.next = first;
        return first;
    }
    
    1. only I
    2. only II
    3. only III
    4. I and II, but not III
    5. II and III, but not I
  2. The intent of the method below is to delete the last node in the linked list to which the parameter first refers:

    public static void removeLast(Node first) {
        Node p = first;
        Node q = p.next;
    
        while (q.next != null) {
            p = q;
            q = q.next;
        }
    
        p.next = null;
    }
    

    Which of the following describes the subset of linked lists for which this method works correctly?

    1. no linked lists
    2. all nonempty linked lists
    3. all linked lists with more than one node
    4. the empty list and all linked lists with more than one node
    5. all linked lists
  3. A doubly linked list is constructed from nodes that are instances of the following class:

    public class DNode {
        private char ch;
        private DNode next;
        private DNode prev;
    }
    

    The next field holds a reference to the next node in the linked list, and the prev field holds a reference to the previous node in the linked list.

    Below is a list of three of these nodes, along with two reference variables, n and p, that refer to specific nodes in the list:

    Which of the following expressions does not refer to the third node in the list?

    1. p.next
    2. n.next.next
    3. p.prev.next
    4. p.prev.next.next
    5. n.next.next.prev.next
  4. The diagram below suggests how we could implement a linked list in which we maintain a reference to both the first and last nodes in the linked list:

    Which of the following operations would be inefficient to carry out when there are a large number of elements in the linked list?

    1. insertion at the end to which front refers
    2. insertion at the end to which rear refers
    3. deletion from the end to which front refers
    4. deletion from the end to which rear refers
  5. The diagram below shows a linked list of characters that is constructed from instances of the StringNode class from lecture:

    In addition, we have included two variables that each store a reference to one of the nodes in the linked list, along with the memory address of each node. You should assume that the ch field has the same address as the node itself, and that the address of the next field is 2 bytes after the beginning of the node.

    1. What is the memory address of the field given by the expression head.next.next?

    2. What is the value of the expression head.next.next?

    3. Write one or more lines of code that remove the node containing the character 's' from the list.

    4. Modify the diagram to reflect the results of executing the following lines on the original version of the list (before the 's' was removed):

      q = q.next;
      q.next = head;
      
  6. This question involves linked lists of integers that are constructed from nodes of the following class:

    public class Node {
        private int val;
        private Node next;
    }
    
    1. Write a static method named numEvenRec() that takes a reference to the first node in a linked list of integers and uses recursion to determine and return the number of even values in the list.

    2. Write a static method named numEvenIter() that takes a reference to the first node in a linked list of integers and uses iteration to determine and return the number of even values in the list.

  7. Write a static method called everyOther() that is a member of the StringNode class. It should take a reference to the first node in a linked-list string, and it should create and return a reference to a new linked-list string that contains every other character from the original string. The original linked list should not be modified. You may use either recursion or iteration.

  8. Consider the following method:

    public static void addAllTo(String[] names, List myList) {
        for (int i = 0; i < names.length; i++) {
            myList.addItem(names[i], i);
        }
    }
    

    The method takes an array of strings and an instance of one of the classes that implement the List interface from lecture – either an ArrayList or an LLList – and it adds all of the strings in the array to the list.

    You may assume that the list is initially empty, and that it is possible to successfully add all of the strings in the array to the list without running out of room.

    From the perspective of time efficiency, does it matter which type of List is passed in – ArrayList or LLList? Explain your answer briefly.

  9. A stack s of integers initially contains the following data from top to bottom:

    {6, 2, 7, 3}
    

    The following code is then executed:

    int x = s.pop();
    int y = s.pop();
    int z = s.pop();
    s.push(x + y);
    int w = s.pop();
    s.push(w + z);
    

    After this code has been executed, what are the contents of the stack?

    1. {8, 10}
    2. {10, 8}
    3. {15, 3}
    4. {10, 8, 6, 3}
    5. none of the above
  10. Suppose that items A, B, C, D and E are pushed, in that order, onto an initially empty stack S. S is then popped four times; as each item is popped off, it is inserted into an initially empty queue.

    If two items are then removed from the queue, what is the next item that will be removed from the queue?

    1. item A
    2. item B
    3. item C
    4. item D
    5. item E
  11. If the keys in the tree below are printed using a postorder traversal, what will the result be?

          G
        /   \
      E       F
     / \     / \ 
    A   B   C   D
    
    1.   G E F A B C D
    2.   G E A B F C D
    3.   A E B G C F D
    4.   A E B C F D G
    5.   A B E C D F G

Questions 12 and 13 involve binary trees whose nodes have the following structure:

public class Node {
    public char val;
    public Node left;
    public Node right;
}
  1. Consider the following method:

    public static int mystery(Node root) {
        if (root != null) {
            if (root.left == null && root.right == null) {
                return 0;
            }
    
            int sum = mystery(root.left) + mystery(root.right);
            if (sum == 0) {
                Node temp = root.left;
                root.left = root.right;
                root.right = temp; 
            }
        }
    
        return -1;
    }
    

    What does this method do to the tree whose root is referred to by the parameter root?

    1. swaps the left and right subtrees of the root node
    2. swaps any two leaf nodes that have the same parent
    3. swaps the leftmost and rightmost leaves
    4. replaces the tree with its mirror image
    5. none of the above
  2. Consider the following methods:

    public static void preorder(Node root, Stack<Character> s) {
        if (root != null) {
            s.push(root.val);
            preorder(root.left, s);
            preorder(root.right, s);
        }
    }
    
    public static void postorder(Node root, Stack<Character> s) {
        if (root != null) {
            postorder(root.left, s);
            postorder(root.right, s);
            root.val = s.pop();
        }
    }
    

    If root initially points to the root of the following tree:

          A
        /   \
      B       C
     / \     / \ 
    D   E   F   G
    

    what tree results from the execution of the following statements?

    Stack<Character> s = new LLStack<Character>();
    preorder(root, s);
    postorder(root, s);
    
    1.           A
              /   \
            B       C
           / \     / \ 
          D   E   F   G
      
    2.           A
              /   \
            C       B
           / \     / \ 
          G   F   E   D
      
    3.           G
              /   \
            D       F
           / \     / \ 
          A   B   E   C
      
    4.           A
              /   \
            C       B
           / \     / \ 
          F   G   D   E
      
    5.           G
              /   \
            F       D
           / \     / \ 
          E   C   B   A
      
  3. If a binary search tree is not allowed to have duplicates, there is more than one way to delete a node in the tree when that node has two children. One way involves choosing a replacement node from the left subtree. If this is done, which node are we looking for?

    1. the largest node in the subtree
    2. the smallest node in the subtree
    3. the root of the left subtree
    4. the next-to-smallest node in the subtree
    5. it doesn’t matter – any node in the left subtree will do
  4. The binary search tree shown below was constructed by inserting a sequence of items into an empty tree.

    Which of the following input sequences will not produce this binary search tree?

    1.   5, 3, 4, 9, 12, 7, 8, 6, 20
    2.   5, 9, 3, 7, 6, 8, 4, 12, 20
    3.   5, 9, 7, 8, 6, 12, 20, 3, 4
    4.   5, 9, 7, 3, 8, 12, 6, 4, 20
    5.   5, 9, 3, 6, 7, 8, 4, 12, 20
  5. If the keys in the tree below are printed using a preorder traversal, what will the result be?

           6
        /     \
      17       22
     /  \     /  \ 
    9    4   16   12
    
    1.   9 4 17 16 12 11 6
    2.   9 17 6 4 16 22 12
    3.   6 9 17 4 16 22 12
    4.   6 17 22 9 4 16 12
    5.   6 17 9 4 22 16 12
  6. A binary tree is constructed of nodes that are instances of the following class:

    public class Node {
        public char val;
        public Node left;
        public Node right;
    }
    

    Consider the following method:

    public static Node mystery2(Node root) {
        if (root.right == null) {
            return root;
        } else {
            return mystery2(root.right);
        }
    }
    

    You consult three consultants, and you get the following three opinions about what the method does when passed a reference to the root node of a binary tree:

    • I. It returns the last node visited by an inorder traversal.
    • II. It returns the last node visited by a postorder traversal.
    • III. It returns the last node visited by a level-order traversal.

    Which of these opinions is correct regardless of the contents of the tree?

    1. I only
    2. II only
    3. III only
    4. I and III
    5. II and III
    1. Create a 2-3 tree from the following sequence

      45, 1, 25, 12, 26, 44, 4, 50, 43, 21
      
    2. Draw a new tree for each of the following operations

      • insert 5
      • insert 42
    3. What numbers do I have to visit if I’m searching for

      • 44
      • 18
  7. Recall our generic Stack interface:

    public interface Stack<T> {
        boolean push(T item);
        T pop();
        T peek();
        boolean isEmpty();
        boolean isFull();
    }
    

    Write a static client method numSmaller(Stack<Integer> stack, int val) that takes a stack of integers and an integer val and that returns the number of values in the stack that are smaller than val. After the method returns, the items in the stack should be in the same positions as they were at the beginning of the method. Your method may use either another stack or a queue to assist it.

  8. The following method uses recursion to search for a key in the binary search tree whose root node is referred to by the parameter root. If it finds the key, it returns a reference to the corresponding data item. If it doesn’t find it, it returns null.

    public static Object search(Node root, int key) {
        if (root == null) {
            return null;
        } else if (key == root.key) {
            return root.data;
        } else if (key < root.key) {
            return searchTree(root.left, key);
        } else {
            return searchTree(root.right, key);
        }
    }
    

    Rewrite this method so that it uses iteration instead of recursion.

Last updated on November 24, 2026.