1
e02
CS56 F16
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu

EXAM: e02: Midterm Exam

ready? date points
true Wed 11/16 12:30PM

You may not collaborate on this exam with anyone. If you need to use the restroom, you must leave your cell phone with the exam proctor before leaving the room.

  • Write your name at the top of this page AND EVERY ODD NUMBERED PAGE.
  • Double check that you turned in ALL pages; look for "End of Exam" on the last page.
  • This exam is closed book, closed notes, closed mouth, cell phone off.
  • You are permitted one sheet of paper (max size 8.5x11") on which to write notes.
  • This sheet will be collected with the exam, and might not be returned.
  • Please write your name on your notes sheet.

The first two questions are essay/short answer type of question, with an emphasis on “short answer”. For this and all other similar questions on this exam, answer as if you were in a job interview. Your answer will be graded on the basis of whether it would be likely to help you or hurt you in that context.
Answers gain or lose points based on whether they are accurate, precise, concise, relevant, and whether they address the specific question asked.

  1. (8 pts) What is a "Design Pattern"? (See the note above about keeping essay/short answer questions short and to the point.)
  2. (8 pts) What is one of the benefits of having good test coverage for your code? (Be sure that your answer demonstrates that you understand what “test coverage” means)

    (See the note on the first page about keeping essay/short answer questions short and to the point.)

  3. For this question, you need the additional handout with code for these files: Book.java, Product.java, Shippable.java, Song.java. These are classes used by the startup E-Commerce company “Amazoony.com”

    You may assume that all of the code for these files on the handout compiles—I’ve checked that this is true. Now consider the code for the Amazoony class.

    Please do these things with this “broken” code for the Amazoony class:

    1. (10 pts) Several lines need to be eliminated from this file in order to make it compile. Find the lines that are bogus, and draw a line through each of them in the code listing above. Hint: By “several”, I mean more than 2, and fewer than 10. Start by determining which, if any, of the constructors are bogus. Then, eliminate any lines that refer to the variables created on those lines. Finally, check all of the remaining method calls.

      You will lose two points each time you striking a line that is not bogus, and you will lose two points for failing to strike any line that IS bogus. So, choose wisely.

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      
      public class Amazoony {
        public static void main (String [] args) {    
          Song lf = new Song("Queen Latifah","Ladies First");
          Book rts = new Book("Julia Alvarez","Return To Sender",949,0.15);
          Product cstf = new Song("Justin Timberlake","Can't Stop The Feeling",79);
          Product fl = new Product("Drake","Fake Love");
          Shippable inf = new Shippable("Dan Brown","Inferno",999,1.5);
          Shippable tbt = new Book("Gwen Ifill","The Breakthrough",652,1.5);
          
          System.out.println("a:" + lf.getTitle());
          System.out.println("b:" + rts.getArtist());
          System.out.println("c:" + cstf.getPrice());
          System.out.println("d:" + fl.getPrice());
          System.out.println("e:" + inf.getTitle());
          System.out.println("f:" + tbt.getPrice());
          System.out.println("g:" + lf.getWeight());
          System.out.println("h:" + rts.getTitle());
          System.out.println("i:" + cstf.getPrice());
          System.out.println("j:" + fl.getPrice());    
        } // main method
      } // class Amazoony
    2. (4 pts) After striking through the bogus lines, the remaining code should compile and run. So, indicate what the output will be (if any) below. Be precise. If there will no output, write “no output”.

  4. Java has two keywords that differ in only a single letter: throw and throws. Using the checked exception java.io.FileNotFound as an example, provide an short example of how each of these keywords might be used.

    Provide both at least one line of java code that contains the keyword throw or throws, as appropriate, referencing IOException, and then a brief explanation of the context of that line of Java code, and what either throw or throws means in that context.

    You may add additional lines of Java code either above and/or below that line of code if they help to provide additional context—but that is optional, not required. What I’m looking for is the shortest code example that clearly illustrates your explanation of the proper use of the two keywords.

    1. (3 pts) Code example for throw

    2. (3 pts) Explanation of code example for throw

    3. (3 pts) Code example for throws

    4. (3 pts) Explanation of code example for throws

    5. (3 pts) Three additional points will be awarded by the grader based on whether the entire explanation above, taken as a whole, clearly illustrates an overall understanding of checked exceptions in Java and the difference between throw and throws.

      If your explanation above already makes that clear, you don’t need to add anything here. If you feel that an additional clarifying remark is needed, write at most one or two sentences.

  5. For this question, you need the additional handout with code for the class ButtonDemo.java.

    You may assume that all of the code in ButtonDemo.java compiles and runs successfully and produces a swing GUI that looks like this when it first loads (I’ve verified that this is true):

    ButtonDemo.java.gui.png

    1. (10 pts) On line 19 you see a call to addActionListener that references a named inner class. Suppose we remove line 19, and replace it with the code that you write in the space below (which could be a single line, or multiple lines, as needed.) Write some code that will do the same thing that will accomplish the same thing as the current line 19, but using a lambda expression instead of an instance of a named inner class.

    2. (10 pts) Now, the same idea, but this time, replace the code on line 21 with equivalent code that references an instance of an anonymous inner class.

  6. (10 pts) (CONTINUED FROM PREVIOUS PROBLEM). A third option for the event handler for a button is to use the class that contains the button—in this case, the class ButtonDemo itself—as the event handler. This is normally used when we have only a single event handler, but it is nevertheless possibly to use this technique in combination with other approaches.

    In the space below, assuming that we start with the original unmodified code for ButtonDemo.java from the handout, explain the changes that would be needed in order to replace the call on line 19 that sets up the named inner class Listener1 as the the event handler for the Button with one that uses the object ButtonDemo itself as the event handler.

    Explain which lines of code you would eliminate, and what you would replace them with. Here is an example of the format your answer should take (this is just the format; it is very far from a correct answer!)

    Replace line 5 with
       public class ButtonDemo extends PotatoChips {
    
    Insert above line 31, this method:
    
       public void handleEvent(int status) {
         System.out.println("I got this");
       }
    
  7. (10 pts) Write the code for the following static method.

    The method takes an plain old java array of int (i.e. an int [] value) and returns an ArrayList<Integer> value.

    public static ArrayList<Integer> arrayToArrayList(int [] array) {
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    }
    
  8. In lab06, we explored the explored some challenges of working with legacy code, plus the basic ideas of how a compiler/interpreter works.

    The code we were working with started with an arithmetic expression such as 2 + 30 * 10 and transformed it, through a series of steps, into the integer 302. The three steps are known as tokenization, parsing, and evaluation.

    1. (5 pts) In tokenization, we start with our input as a string, e.g. 2 + 30 * 10, and transform it into something else, something that is the input to the parsing phase.

      Briefly describe this process, and the nature of the output of tokenization (which is the input to parsing). For full credit, refer to the specific string string 2 + 30 * 10 as an example.

    2. (5 pts) Similarly, parsing takes the output of the tokenization phase, and transforms it into something else; something that is the input to the evaluation phase (also sometimes called the “interpretation phase”).

      Describe this process, and the form of the output of parsing, making specific reference to what would happen to the string 2 + 30 * 10, and what data structure is produced by the parsing phase (as input to the evaluation phase).

    3. (5 pts) Finally, evalution takes the output of the parsing phase and transforms that into an answer (in the case of our program, that answer is an integer, e.g. 302 for the input 2 + 30 * 10.

      Describe this process, making reference to the data structure that is passed off from the parsing phase to the evaluation phase.

End of Exam