CSCI 180 Spring 2008, Midterm Exam Practice

Sample Questions

  1. A rational value is one with an integer numerator and positive integer denominator. Create a class Rational with

  2. Short answer: use the following class definitions in this question, as necessary:
    public class Bug {
        private int numLegs;
        private static final int SPIDER_LEGS = 8;
        private static final int INSECT_LEGS = 6;
        public Bug(int n){
            numLegs=n;
        }
        public boolean isInsect(){
           boolean result=false;
           if (numLegs==INSECT_LEGS){
              result=true;
           }
           return result;
        }
    }
    
    public class Hive {
        private Bug queen;
        private Bug [] workers;
        public Hive(int n) {
            workers=new Bug[n]; // line 1
    	queen = new Bug(6); // line 2
            ...
        }
        private void replaceQueen(Bug q){
           ...
        }
    }
    
    1. T/F: queen.numLegs = 5; would be a legal statement to add to the constructor of the Hive class. (Explain)
    2. T/F: queen is an instance variable. (Explain)
    3. T/F: The variable result could be referenced from a method inside of class Hive. (Explain)

    4. Explain the difference between the line marked "line 1" and the one marked "line 2" in terms of what is being created in each.
    5. Explain why someone might use a local variable rather than an instance variable.
    6. Explain why a method would use a parameter.
    7. Explain the difference between a white-box test and a black-box test.
    8. Explain the difference between a HashMap and a HashSet.

  3. Find as many errors as you can in the code below:
    public class Student {
        String name;
        String id;
        
        public Student(String name, String id){
            name=name;
            id = id;
       }
       public String getName() {
            return name;
        }
        public String getID (){
            return id;
        }
    }
    import java.util.ArrayList;
    import java.util.Iterator;
    
    public class Registrar {
        private ArrayList studentList;
        public Registrar(){
            studentList = new ArrayList ();
        }
        public void enrollStudent (Student newStudent){
            Student student=newStudent;
            studentList.add(student);
        }
        public Student findStudent(String targetID) {
            Iterator studentIT = studentList.iterator();
            Student current = null;
            boolean found = false;
            
            while ((studentIT.hasNext()) && (!found)){
                studentIT.next();
    	    current = (Student)studentIT.next();
                if (targeted == current.getID()){
                    found=true;
                }
            }
            return current;
        }
        public Student removeStudent(String targetID){
            boolean found=false;
            Student current = null;
            for (int i=0; i < studentList.size(); i++){
                current = (Student) studentList.get(i);
                if (current.equals(targetID)){
                    found=true;
                    studentList.remove(i);
                }
            }
            
            return current;
        }
            
    }
    
  4. What is the output from the following code if we pass "this is a test" to the constructor in creating a Test1 object and then call doSomethingMore() on that object?
    import java.util.ArrayList;
    public class Test1
    {
        private int [] mystery1;
        private String string;
        private ArrayList mystery2;
    
        public Test1(String str)
        {
            mystery1=new int[str.length()];
            for (int i=0; i < mystery1.length; i++){
                mystery1[i]=0;
            }
            string=str;
            mystery2 = new ArrayList();
        }
        public void doSomething(){
            for (int i=0; i < string.length(); i++){
                String mystery3=string.substring(i, i+1);
                boolean x=false;
                for (int j=0; j < mystery2.size(); j++){
                    if(mystery2.get(j).equals(mystery3)){
                        x=true;
                        mystery1[j]++;
                    }
                }
                if(!x){
                    mystery1[mystery2.size()]++;
                    mystery2.add(mystery3);
                }
            }
        }
        public void doSomethingMore(){
            doSomething();
            for (int i=0; i < mystery2.size(); i++){
                System.out.println(mystery2.get(i)+": "+mystery1[i]);
            }
        }
    }
    

  5. What happens if we call doSomethingMore() again on the same Test1 object? What is the output?

  6. Consider the CalendarDate class and the method
    boolean isLaterThan(CalendarDate comparisonDate)
    1. When specifying a test case, what information would we need to give?
    2. List a set of test cases that you think would convince a user that the method is correct.