Sample Midterm Test

CSCI 2341


The questions on this sample test were extracted from an actual midterm. Because that midterm occurred later in the term (relative to this year) it covered more topics.

The actual midterm was about twice this length.

Your midterm next week will have more questions than this, as well.

The questions are provided only to give you some idea of the kinds of questions I ask: short answers, writing code, and multiple choice.


  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?

     

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

     

     

     

     

     

  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.

     

     

     

  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.

     

     

     

     

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

     

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