1
e03
CS56 F17
DO NOT WRITE IN THIS AREA! Name: Seat:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu

EXAM: e03: Midterm Exam

ready? date points
true Thu 12/08 12:00PM

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.

  1. (6 pts) For each of the following indicate if the line of code involves auto-boxing, and/or auto-unboxing. If a line of code involves both, check both boxes. If it involves neither, check neither box. ASSUME THAT ALL THE LINES OF CODE ARE IN THE SAME main METHOD, CONSECUTIVELY.

    (Grading: -1 for each incorrect answer, but no more than -6 total.)

    Code auto-boxing auto-unboxing

    ArrayList<Integer> mylist = new ArrayList<Integer>();

    mylist.add(new Integer(4));

    int x = mylist.get(0) + 1;

    mylist.add(new Integer(x));

    int x = mylist.get(1);

    Integer y = mylist.get(new Integer(0));

  2. We are using a third-party library called JUnit in this course. If you were asked at a job interview to briefly describe both the purpose of JUnit, and how it is used what would you say?

    Include enough detail in your answer so that the interview knows that you are technically sharp, and they should hire you. Do not include so much extra detail that the interviewer finds you tedious and annoying, and decides you would be painful to work with, and chooses to not hire you.

    1. (5 pts) Purpose of JUnit? (Why would someone use JUnit? What are the benefits?)
    2. (5 pts) How it is used? (What do you do in order to use JUnit, including at least some mention of setup, workflow and the code you write?)
  3. Object Line of code
    (a) Fido  
    (b) Princess  
    (c) Rover  
    (d) Snoopy  
    (e) Spot  

    (5 pts) On the handout there is some code. Your job: figure out after which line of main() each of the following objects is eligible for garbage collection.

    If an object is still not eligible for garbage collection when the last line of main is reached, write “never”. Each answer should be a line number, or the word never.

  4. 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 “Amazany.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 Amazany class.

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

    1. (4 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 one point each time you striking a line that is not bogus, and you will lose one point 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 Amazany {
      public static void main (String [] args) {
      Shippable lf = new Song("Queen Latifah","Ladies First");
      Shippable rts = new Shippable("Julia Alvarez","Return To Sender",949,0.15);
      Product cstf = new Song("Justin Timberlake","Can't Stop The Feeling",79);
      Shippable fl = new Song("Drake","Fake Love");
      Product inf = new Book("Dan Brown","Inferno",999,1.5);
      Book tbt = new Book("Gwen Ifill","The Breakthrough",652,1.5);

      System.out.println("a:" + lf.getArtist());
      System.out.println("b:" + rts.getPrice());
      System.out.println("c:" + cstf.getTitle());
      System.out.println("d:" + fl.getWeight());
      System.out.println("e:" + inf.getArtist());
      System.out.println("f:" + tbt.getTitle());
      System.out.println("g:" + lf.getPrice());
      System.out.println("h:" + rts.getWeight());
      System.out.println("i:" + cstf.getPrice());
      System.out.println("j:" + fl.getTitle());
      } // main method
      } // class Amazany
    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”.

      You will lose one point for each line of either missing, or incorrect output. A line must be perfect to be considered correct. So be precise.

  5. On the handout you got for this exam, there is a Book class. The Book class, as written, lacks an overridden .equals method.

    Yet, it is still possible to invoke .equals() on Book objects.

    Briefly, but preicsely, answer these questions.

    1. (5 pts) The Book class, as written, lacks an overridden toString method.

      Write the code for an overridden toString method the returns the title of the book.

    2. (5 pts) When overriding methods such as .toString, .equals or .hashCode, it is considered good practice to use the annotation @Override.

      (Hint: If you didn’t include @Override in your answer to the previous question, go back and put it there now.)

      As we discussed in lecture, the annotation @Override helps the compiler to signal errors the programmer could make that might otherwise not be noticed at compile time.

      Assuming the programmers intention is to override the toString method inherited from java.lang.Object, give a specific example of a mistake the programmer might make that would NOT be flagged by the compiler unless the programmer included the @Override annotation.

      By specific example, I mean, write the incorrect code.

  6. (5 pts) Continuing with the Book class from the handout: handout

    1. (5 pts) Write two lines of code that declare two variables a and b, each of which is a Book reference, and initialize both to separate Book objects with identical values for all the attributes of a Book.

    2. Output for part (b)

       

      (5 pts) Assuming .equals is not overridden, what would the output of this code be? Answer in the box at the right hand side.

        if (a==b) {
          System.out.println("W");
        } else {
          System.out.println("X");
        }
        if (a.equals(b)) {
          System.out.println("Y");
        } else {
          System.out.println("Z");
        }
      
    3. (5 pts) Write the code for an overridden .equals method for Book that compares only the title attribute of Book to determine equality.

      The reverse side of the ArrayList handout has some reminders about a properly written .equals() method.

    4. (5 pts) Continuing with the Book class: if/when you override the .equals() method for a class, it is considered a “Java best practice” to always override the hashCode method also. Given the implementation of .equals from the previous problem, write an overridden hashCode method for Book. (As a hint, note that the class java.lang.String already has a properly written built in hashCode method.)

  7. Java provides two types of exception. One of those is created by extending java.lang.Exception, and the other by extending java.lang.RunTimeException.

    Suppose you were creating a Java application that runs on a smartphone, and reads data from a personal fitness device (e.g. something like a FitBit) using BlueTooth. Suppose the device is being developed by a lab at UCSB called Gauchofit.

    The device sends messages, the first 32 bits of which are always an integer containing the “message type”. At the moment, there are 11 message types defined (values 0 through 10), but more could be defined in the future.

    Your job is to create two new exception classes in the package edu.ucsb.gauchofit:

    • The exception edu.ucsb.gauchofit.DeviceNotReady should be thrown any time the software detects that communication can’t be estabished with the device, or has been lost. It is not possible for the programmer to predict when and if this exception will occur.

    • The exception edu.ucsb.gauchofit.UnknownDataType should be thrown any time a message from the device has a value for the message type that is outside the legal range. As long as the code is written correctly, and the communications protocols are working as they should, this should never happen; it represents a bug in the software if it does occur.

    1. (4 pts) Apart from any comments, what should the first line of the file DeviceNotReady.java be?

    2. (4 pts) Assuming that source code is stored under a directory called src/, list the full file name for DeviceNotReady.java, including all directories between src and DeviceNotReady.java, if any.

    3. (4 pts) The first line of the class declaration for DeviceNotReady will start with public class and then continue.

      What code will follow those words, up to and concluding with the open brace ‘{‘ ? (You do not need to write the entire class.)

    4. (4 pts) Same question, but for the class UnknownDataType. What follows public class in UnknownDataType.java, concluding with the open brace {?

  8. This question, and the one that follows are essay/short answer type questions, 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.

    (10 pts) Choose ONE (only one) of the following design patterns:

    1. Strategy
    2. Observer
    3. Decorator
    4. Factory

    then describe each of the following:

    • The specific situation/context in which the pattern is useful
    • The relationships between/among classes/interfaces/objects
    • The way in which this pattern makes code easier to understand and maintain

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

  9. (10 pts) You have been hired at an internship that is a “try out” for a full time position. Your team leader tells you:

    “One of the reasons we’ve hired you is that we see you have a lot of experience with git/github. We are hoping that you can help bring the other interns up to speed, since they have less experience with git/github. Could you write up a short “cheat sheet” with just the most essential information about git/github that they need to know? It should include the commmands they need, as well as some instructions about when to use each of those commands. It has to fit in the space below. Don’t write too small; it needs to be clear and legible.”

    In the space below, put your “cheat sheet” for git. Choose carefully what to include, as well as what to leave out, since you can’t possibly write everything you know about git/github in the space below.

    Don’t feel you need to cram the sheet full. Neatness and legibility are more important to usefulness than filling in everything you know. Prioritize.

 

End of Exam