CS365 Final -- Spring 2012

  1. This exam is open-note, open book. You may not use a computer or any electronic devices.
  2. You must answer all of the questions.
  3. Answer all the questions on the exam itself and hand in the exam.
  4. Well written code (e.g., with good variable names) will receive more credit than poorly written or poorly organized code.
  5. Well written and well organized explanations will receive more credit than poorly written or poorly organized explanations.
  6. Good luck!

  1. Names, Scopes, and Bindings
    1. Both early and late binding confer certain advantages on a program. One example of early versus late binding is C/C++'s decision to do static linking by placing all libraries in the same object module and Java's decision to do dynamic linking by loading class files on demand at run-time. Describe the advantages that accrue respectively to C/C++'s early binding decision on linking and to Java's late binding decision on linking.
      
      
      
      
      
      
      
      

    2. Behold the following program:
      string player1 = "scissors";
      
      string winner(string player2) {
          if (player1 == "rock") {
              if (player2 == "scissors")
                  return player1;
              else if (player2 == "paper")
                  return player2;
              else
                  return "tie";
          }
          else if (player1 == "scissors") {
              if (player2 == "rock")
                  return player2;
              else if (player2 == "paper")
                  return player1;
              else
                  return "tie";
          }
          else { // player1 selected paper
              if (player2 == "rock")
                  return player1;
              else if (player2 == "scissors")
                  return player2;
              else
                  return "tie";
          }
      }
      
      int main() {
        string player1 = "rock";
        string player2 = "paper";
        cout << winner(player2) << endl;
      }
      
      1. What result gets printed by this code under static scoping? Why?
        
        
        
        
      2. What result gets printed by this code under dynamic scoping? Why?

  2. Data Types and Type-Checking

    1. What is the difference between a statically type checked language and a strongly type checked language?
      
      
      
      
      
      
      
    2. Give one simple code example from C++ that shows that C++ is not strongly typed.
      
      
      
      
      
      
    3. Consider the following type declarations:
      struct Date {
         int month;
         int day;
         int year;
      };
      
      struct Time {
         int hours;
         int minutes;
         int seconds;
      };
      
      typedef Date arrivalDate;
      typedef Date departureDate;
      typedef Time elapsedTime;
      
      struct Date admissionDate, graduationDate;
      struct Time startTime;
      arrivalDate HawaiiArrival;
      departure Date HawaiiDeparture;
      elapsedTime runnersTime;
      
      Which of these variables are type equivalent using
      1. structural type equivalence
        
        
        
      2. loose name equivalence
        
        
        
      3. strict name equivalence
        
        
        
  3. Event Programming and Graphical User Interfaces

    1. If I want a user to enter a bounded integer between 0 and 20, why is it better to present the user with a slider instead of a textbox?
      
      
      
      
      
    2. For each of the following user interface scenarios, choose from the following list the type of widget you think is most appropriate. One of the problems has two acceptable answers; the others have only one acceptable answer.
      • slider
      • menu/list
      • type in text box
      • radio button
      • check box
      • command button

      1. _________________ You want the user to select an occuption from a set of 50 possible occupations.

      2. _________________ You want the user to select the number of bedrooms that they are interested in having in a house. The most bedrooms that a house can have is six.

      3. _________________ You want the user to select a font style. The possible options are bold, italic, underline, and oblique. They can select anywhere from zero to all four options.

      4. _________________ You want the user to enter the weight of a package and are willing to accept any weight up to 10000 pounds.

      5. _________________ You want the user to specify a color by entering the values for red, green, and blue as integers between 0 and 100. The user should be able to rapidly change the values and see the current color immediately updated in a color swatch.

  4. Model-View-Controller (MVC) model: Behold the following Java pseudo-code for displaying and updating the results of an election:
    class ElectionResults extends JPanel {
        int candidate1voteTotal = 0;
        int candidate2voteTotal = 0;
        int candidate3voteTotal = 0;
        int candidate4voteTotal = 0;
        JFrame window = new JFrame();
        
        void addToCandidate1(int votes) {
            candidate1voteTotal += votes;
            repaint();
        }
    
        void addToCandidate2(int votes) {
            candidate2voteTotal += votes; 
            repaint();
        }
    
        void addToCandidate3(int votes) { ... }
        void addToCandidate4(int votes) { ... }
    
        void initializeView() {
            window.getContentPane().add(this);
        }
        void paintComponent(Graphics g) {
            g.drawText(candidate1voteTotal);
            g.drawText(candidate2voteTotal);
            g.drawText(candidate3voteTotal);
            g.drawText(candidate4voteTotal);
        }
    }
    
    This pseudo-code abstracts away details about making the window visible and where to place the text in a window, so please do not worry about the details. From a broader perspective, the design of this class violates the MVC model. Answer the following questions:

    1. How does this class declaration violate the MVC model?
      
      
      
      
      
    2. How could the design of this class hinder two programmers that want to add two separate views, one that provides a pie chart of the results and one that provides a scrolling ticker with the results?
      
      
      
      
      
      
    3. How could you modify this code so that it adheres to the MVC model and supports additional views. Do not write the pseudo-code. Instead answer the following questions:

      1. Which methods/variables would you move out of ElectionResults and where would you move them?
        
        
        
        
        
      2. Currently you have two general sets of methods for this class--mutator methods and drawing methods. What additional general sets of methods would you need to add to either the existing class or your newly created class(s) to get a clean separation beween their implementations and to allow them to communicate?
        
        
        
        
        

  5. Consider the following Java class definition:
    public class BadThreads {
    
        static String message;
    
        private static class CorrectorThread
            extends Thread {
    
            public void run() {
                try {
                    sleep(1000); 
                } catch (InterruptedException e) {}
                // Key statement 1:
                message = "Mares do eat oats."; 
            }
        }
    
        public static void main(String args[])
            throws InterruptedException {
    
            CorrectorThread t = new CorrectorThread();
            t.start();
            message = "Mares do not eat oats.";
            Thread.sleep(2000);
            // Key statement 2:
            System.out.println(message);
        }
    }
    
    The application should print out "Mares do eat oats." It will do so most of the time, but not all of the time, because there is a happens-before relationship between key statement 1 and key statement 2 that is not always obeyed. Sometimes key statement 1 will not be executed before key statement 2 and sometimes key statement 1 will be executed before key statement 2 but its result will not be visible to the main thread.

    1. How is it possible that key statement 1 could execute before key statement 2 and yet the change to message not be visible to the main thread?
      
      
      
      
      
    2. Java provides at least two different strategies that can be used to ensure that key statement 1 gets executed before key statement 2. Name one of these strategies, along with a quick explanation of why/how the strategy ensures the proper sequencing.
      
      
      
      
      
    3. Suppose you are confident that the sequencing will occur in the right order and want a lighter weight strategy that simply ensures that the change to message will be visible to the main thread. Java provides at least two different strategies that can be used to ensure that the results of key statement 1 will be made visible to the main thread. Name one of these strategies, along with a quick explanation of why/how the strategy ensures the proper sequencing.
      
      
      
      
      

  6. Functional Programming

    1. Name two essential elements of pure functional programming that differentiate it from imperative programming.
      
      
      
      
      
      
    2. What recursive programming technique can a programmer use to help a functional language compiler improve the efficiency of the compiled code? Explain how the compiler can take advantage of this technique to produce more efficient code.
      
      
      
      
      
      
      
      
      
      
    3. Using Scheme, write a function named split that takes a list as a parameter, splits the list in two, and returns the two split lists as a cons pair. If the list has an odd number of elements, place the additional element in the first split list. Your answer may not use either iteration or side-effects. Hint: There is not a Scheme operator for splitting a list in the middle, so your function should recursively assign the first two elements of the list to the lists split lists you are creating, and then recurse on the remainder of the list. I do not care whether or not you use tail recursion.