JMU
Sample Questions for the Final Exam


  1. Answer the sample questions for and .
  2. Indicate whether each of the following statements is true or false:
    (1) _____ 2-dimensional arrays must be rectangular.
    (2) _____ The elements in an array must be homogeneous.
    (3) _____ Arrays use linked memory.
    (4) _____ An ArrayList can not be re-sized.
    (5) _____ A checked exception is derived either from Error or RuntimeException.
  3. Discuss the differences and/or similarities between a "first-in, first-out" collection and a "first-in, last-out" collection.
  4. Draw a model of how memory is allocated by the following code snippet. Carefully illustrate what happens in statement 1, statement 2, and statements 3 through 6.
    1  Integer[]        values;
    
    2  values = new Integer[5];
    
    3  for (int i=0; i < values.length; i++)
    4  {
    5      values[i] = new Integer(i*10);
    6  }
      
  5. Given the following implementation of an IntNode:
    public class IntNode
    {
        public int          value;
        public IntNode      next;
    }
      

    and the following implementation of a Pew:

    public class Pew
    {
        private IntNode last;
    
    
        public Pew()
        {
    	last  = null;
        }
    
        public void pish(int anInt)
        {
    	IntNode temp;
    
    	temp = new IntNode();
    
    	temp.value = anInt;
    	temp.next  = last;
    
    	last = temp;
        }
    }
      

    draw a model of memory that illustrates what happens when the following statements are executed:

      Pew   p;
    
      p = new Pew();
    
      p.pish(22);
    
      p.pish(33);
    
      p.pish(44);
      
  6. Given the following Person class:
    public class Person
    {
        public int       age;
        public Person    bestFriend;    
        public String    name;    
    
        public Person(String name, int age, Person bestFriend)
        {
           this.name       = name;
           this.age        = age;
           this.bestFriend = bestFriend;       
        }   
    
        public String toString()
        {
           String     s;
           
           s = name + " (" + age + ") ";
           if (bestFriend == null) s += "has no friends";
           else                    s += "is friends with " + bestFriend.name;
    
           return s;       
        }
        
    }
        

    what will be printed by the following Driver (assuming everything is compiled and executed properly)? Note: Be careful!

    import java.util.*;
    
    
    public class Driver
    {
        public static void main(String[] args)
        {
           Person       alice, bob, carol, dan, temp;
           ArrayList    people;
           
    
           alice = new Person("Alice", 30, null);
           bob   = new Person("Bob",   32, alice);
    
           System.out.println(alice.toString());
           System.out.println(bob.toString());
           
           carol = new Person("Carol", 40, null);
           dan   = new Person("Dan",   50, carol);
           carol.bestFriend = dan;
    
           System.out.println(carol.toString());
           System.out.println(dan.toString());
    
           people = new ArrayList();
           people.add(alice);
           people.add(bob);
           people.add(carol);
           people.add(dan);
           
           alice.age      = 65;
           bob.bestFriend = carol;
           carol.name     = "Carol Ann";
           dan.age        = 88;
           
           
           for (int i=0; i<people.size(); i++)
           {
              temp = (Person)people.get(i);
              System.out.println(temp.toString());          
           }
           
    
        }
    }
        
  7. Modify the Driver above so that it explicitly uses an ArrayList of Person objects (and is type safe).
  8. Modify the Person class above as follows:
    • Make all of the attributes private.
    • Add a setName(String name).
    • Add a setAge(int age) method that throws a NobodyIsThatOldException whenever the age is greater than 39. This exception must be checked. (Note: You will need to write a NobodyIsThatOldException class.)
  9. Modify the Driver class so that it now works with this new version of the Person class.
  10. Given the following Printer class:
    import java.util.*;
    
    public class Printer
    {
        private StringTokenizer         tokenizer;
        
    
        public void print(String line)
        {
    	tokenizer = new StringTokenizer(line, ",");
    
    	if (tokenizer.hasMoreTokens())
    	{
    	    printTokens(tokenizer);
    	}
        }
    
    
        private void printTokens(StringTokenizer tokenizer)
        {
    	String      token;
    
    	token = tokenizer.nextToken();
    	if (tokenizer.hasMoreTokens())
    	{
    	    printTokens(tokenizer);
    	}
    
    	System.out.println(token);
        }
    
    }
      

    carefully trace the execution of the following:

    public class PrinterDriver
    {
        public static void main(String[] args)
        {
    	Printer      test;
    
    	test = new Printer();
    	test.print("Aboutabl,Bernstein,Harris");
        }
    }
      
  11. In prefix notation, binary arithmetic expressions are written as follows:

      arithmetic-operator operand1 operand2

    For example, the following are valid expressions in prefix notation:

      + 5 3
      * 2 8

    Suppose we have a Stack class that holds String objects and a method named evaluate in the Calculator class that is passed the three parts of a prefix expression (as String objects) and returns a String representation of the result. Trace the execution of the following, carefully modeling memory at each step:

      Stack      terms;
    
      terms = new Stack();
    
      terms.push("5");
      terms.push("3");
      terms.push("+");
    
      result = Calculator.evaluate(terms.pop(), terms.pop(), terms.pop());
    
      terms.push(result);
      terms.push("4");
      terms.push("*");
    
      result = Calculator.evaluate(terms.pop(), terms.pop(), terms.pop());
      
  12. Assuming the following Adder class is compiled properly:
    public class Adder
    {
    
       public static void main(String[] args)
       {
          double        result;
          
          try 
          {
             result = add(args);
             System.out.println(args[0]+"+"+args[1]+": "+result);
          }
          catch (ArrayIndexOutOfBoundsException aioobe)
          {
             System.out.println("Too few operands.");
          }
       }
    
    
    
    
       public static double add(String[] terms) 
                                         throws ArrayIndexOutOfBoundsException
       {
          double        left, result, right;
          String        operator;
          
          left     = 0.0;
          right    = 0.0;
          
    
          try
          {
             left = Double.parseDouble(terms[0]);
          }
          catch (NumberFormatException nfe)
          {
             left = 0.0;         
          }
          
          try
          {
             right = Double.parseDouble(terms[1]);
          }
          catch (NumberFormatException nfe)
          {
             right = 0.0;         
          }
          
             
          result = right + left;
          
          return result;
       }
    }
    

    carefully trace the execution of each the following:

      java Adder 5 4
      java Adder 5
      java Adder 5 b
      java Adder 3 4 5 6
  13. Complete the total() method in the following class. You must use an Iterator, and you must use it properly.
    import java.util.*;
    
    public class Revenues
    {
        private ArrayList<String>     items;
    
        public Revenues()
        {
            items = new ArrayList<String>();
        }
    
    
        /**
         * Add an item value to the collection of revenues
         *
         * @param item   A dollar value (including the leading $ and commas)
         */
        public addItem(String item)
        {
            items.add(item);
        }
    
    
        /**
         * Calculates the total of all (valid) items in this collection
         *
         * @return   The total revenue
         */
        public double total()
        {
        
    
    
    
    
    
    
    
        }
    }
    
  14. Write a static method that is passed an int[][] and returns true if and only if all of the rows and all of the columns sum to the same value.
  15. Write a static method that is passed a square int[][] of size nxn (i.e., with n rows and n columns) and returns true if and only if it contains all of the int values from 1 to n 2.
  16. Write a class named QueueOfDoubles that, as its name almost implies, contains a queue of double values (not Double objects). It must contain a "copy constructor". It must use a linked implementation.
  17. Write a SavingsAccount class that contains a queue of deposits and withdrawals. It must use a QueueOfDoubles object. Deposits should be represented in the queue as positive entries and withdrawals should be represented in the queue as negative entries. It must have methods for the following:
    • Add a deposit.
    • Add a withdrawal.
    • Print all withdrawals and deposists (with a running balance).
    • Calculate the final/current balance.

    It must be possible to call each method more than once. (Hint: Think about how this can be facilitated using the "copy constructor".)

  18. Write a PersonalAccounts class (to be used by a bank) that contains a HashMap of SavingsAccount objects in which the keys are the names of the account holders. It must have methods for the following:
    • Add a SavingsAccount.
    • Get a SavingsAccount.
    • Calculate the total amount of money in all of the SavingsAccount objects.

Copyright 2011