Sample Midterm Solutiont

CSCI 2341


Answers to the questions appear in blue text. Commentary on the answer appears in red text.


  1. [2] Consider the function shown below, which modifies the Bag given to it.
    public static void modify(Bag<String> bag) {
        Bag<String> newBag;
        while (!bag.isEmpty()) {
            String s = bag.remove();
            if (!newBag.contains(s)) {
                newBag.add(s);
            }
        }
        while (!newBag.isEmpty()) {
            bag.add(newBag.remove());
        }
    } 
    
    Suppose myBag contains "A", "B", "C", "D", "A", "E", and "D". What does myBag contain after modify(myBag) has been called?
    "A", "B", "C", "D" and "E"
    
    The first loop empties bag and puts each element into newBag unless newBag already contains it. Thus only one copy of each element gets into newBag.

    The second loop empties newBag back into bag, leaving it with no duplicates.

     

  2. [8] Create an interface (Unknown) that will hold one object of some specified type - for example an Integer or a String or a Scanner. Each Unknown object can only hold the kind of object it’s supposed to—so you’re not allowed to put a Scanner into an Unknown that holds Integers. The interface has a method to get the object in it, another to set that object, and a third to test whether that object exists.

    Define the interface. (No comments necessary!)

    public interface Unknown<T> {
        public T get();
    
        public void set(T newValue);
    
        public boolean exists();
    }
    
    This is an interface, not a class. Thus it should have no instance variables and the methods should have no bodies. Our style guidelines say that the methods should be declared public but not abstract (even tho' they are both by default).

     

  3. [2] Suppose we have an ArrayBag with the following array holding its contents:
    0123456789
    "Anita""Bill""Chantal""Djeri""Eli""Frank""Gordon"///
    Show the contents of the array after remove("Bill") has been executed.
    0123456789
    "Anita""Gordon""Chantal""Djeri""Eli""Frank"////
    This is the ArrayBag class from the notes. Its behaviour needs to match the behaviour we gave it.

     

  4. [8] Write the count method for the LinksBag class:
    public class LinksBag<T> implements Bag<T> {
        private class Node {
            private Node next;
            private T data;
        }
           
        private Node first; 
    } 
    
    The method counts how many times a given entry appears in the LinksBag object. Do not use either toArray method, nor any method that need to be imported, and do not modify the contents of the bag in any way.

    Note that this class does not have a numInBag field.

    @Override
    public int count(T item) {
        int count = 0;
        Node cur = first;
        while (cur != null) {
            if (Objects.equals(item, cur.data) {
                ++count;
            }
            cur = cur.next;
        }
        return count;
    }
    
    In earlier iterations of this course, I used a count-controlled loop to run thru the contents of the chain.

    That works, but it's better programming practice to use the chain structure itself to control looping along the chain.

     

  5. [4] Suppose we have the following arrangement of nodes in memory (where this is a LinkedBag):

    Show how the structure would look after the command cur.next = first; has been executed by putting X(s) thru old arrow(s) and adding new arrow(s).
    
    
    I've replaced the original image with the modified one.

     

  6. [6] Multiple Choice: select the best available answer from the options shown.