Exam 1 Answers:Section 02

    1. T
    2. F
    3. T
    4. A
    5. D
    6. B
    7. A
    8. C
      1. O(NlogN)
      2. O(N2)
      3. O(1)

    9. Output:
      7
      5
      3
      1
      2
      6
      16
      24
      24
        1. 1
        2. N+1
        3. N(N+1)
        4. N2
        5. N2
        6. N2
        7. N2
        8. N
        9. N
      1. O(N2)
        1. 1
        2. N+1
        3. N(N+1)
        4. N2
        5. N2
        6. N2
        7. 0
        8. N
        9. N
      2. O(N2)

    10.  

      Ford
      Tesla
      BMW
      Toyota
      
    11.    public int diff(LinkedStack<T> two) {
            int numDiff = 0;
            LLNode<T> temp = top;
            LLNode<T> tempTwo = two.top;
            while (temp != null && tempTwo != null) {
               if (temp.getInfo().equals(tempTwo.getInfo())
                  numDiff++;
               temp = temp.getLink();
               tempTwo = tempTwo.getLink();
            }
            if (temp != null)
               while (temp != null) {
                  numDiff++;
                  temp = temp.getLink();
               }
            if (tempTwo != null)
               while (tempTwo != null) {
                  numDiff++;
                  tempTwo = tempTwo.getLink();
               }
            return numDiff;
         }
      
    12. The diff method must access every element of this and every element of the parm. If there are M elements in this and P elements in the parm, then it has to access M + P elements. M and P are the size of the stacks, so if we simplify and consider them the same size, then we get 2N, where N is the size of the stacks. This gives us O(N).
      1.    public String toString() {
              return title + "   " + studio + "   " + genre + "   " + length + " minutes";
           }
        
      2.    public boolean isGenre(String gen) {
              if (genre.equals(gen))
                 return true;
              else
                 return false;
           }
        
      3.    ArrayBoundedStack<Film> action = new ArrayBoundedStack<>();
           ArrayBoundedStack<Film> comedy = new ArrayBoundedStack<>();
        
      4.    int i;
           String title, studio, genre, dummy;
           int length;
           Film inFilm;
           System.out.println("enter the title of the film: ");
           title = keybd.nextLine();
           while (!title.equals("done")) {
              System.out.println("enter the studio name: ");
              studio = keybd.nextLine();
              System.out.println("enter the genre: ");
              genre = keybd.nextLine();
              System.out.println("enter the length of the film: ");
              length = keybd.nextInt();
              dummy = keybd.nextLine();
              inFilm = new Film(title, studio, genre, length);
              if (inFilm.isGenre().equals("action"))
                 action.push(inFilm);
              if (inFilm.isGenre().equals("comedy"))
                 comedy.push(inFilm);
              System.out.println("enter the title of the film: ");
              title = keybd.nextLine();
           }
        
      5.    while (!action.isEmpty()) {
              inFilm = action.top();
              action.pop();
              System.out.println(inFilm);
           }
        
      6.    while (!comedy.isEmpty()) {
              inFilm = comedy.top();
              comedy.pop();
              System.out.println(inFilm);
           }
        
    13.    public static int numOver(int[] arr, int size) {
            if (size == 0)
               return 0;
            else {
               if (arr[size-1] > 100)
                  return 1 + numOver(arr,size-1);
               else
                  return numOver(arr,size-1);
            }
         }
      
      1. There will be size+1 calls, for values size, size-1, size-2,...,0.
      2. 3 for each recursive call, 2 for the base case (size == 0)
      3. 3*size + 2
      4. O(size) or O(N)


    14. Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

      © Copyright Emmi Schatz 2023