Fall 2024 [Bono]

CS 455 Midterm 1 Solution


Problem 1
Part A [8].


Part B [2].

 Yan Sam Jose


Problem 2
A few possible correct solutions. Changes shown in bold.

public static void printSentence(ArrayList words) {

                       words.size()-1
   for (int i = 0; i < words.size(); i++) {

     System.out.print(words.get(i) + " ");

   }

   System.out.print(words.get(words.size()-1);

   System.out.println(".");
}


Problem 3
A few possible correct solutions.

mkdir cs455
mv pa* cs455
mv lab* cs455

Problem 4
Several possible solutions for overlaps. Solution shown in bold.   
// A class to represent a one-dimensional closed interval
public class Interval { 
   // space for instance variables here:

   private double low;
   private double high;
  

   // Creates the interval from low to high
   // PRECONDITION: low <= high
   public Interval (double low, double high) {

      this.low = low;
      this.high = high;

   }

   // Returns the low bound of the interval.
   public double low() {

      return low;


   }

   // Returns the high bound of the interval.
   public double high() {

      return high;

   }

    // Returns true iff this Interval contains the specified point.
    public boolean contains(double point) {

      return low <= point && point <= high;

    }

   

   // Returns true iff this Intervals and the specified Interval overlap (i.e.,
   // contain one or more points in common).
    public boolean overlaps(Interval other) {

      return !((this.high < other.low) || (other.high < this.low));

   }
}


Problem 5
Credit was given for answers given in addition to these. Just showing solution visually here, except where the exact number matters.

Problem 6

  1. yes
  2. no
  3. yes
  4. no
  5. yes
  6. yes
  7. no
  8. no

Problem 7
A few variations depending on how you do your loop variables.

   public static boolean repeatAtEnds(int[] vals, int seqLength) {

      int endInd = vals.length - seqLength;

      for (int startInd = 0; startInd < seqLength; startInd++) {

         if (nums[startInd] != nums[endInd]) { return false; }
         endInd++;

      }

      return true;
   }