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

EXAM: e01: Midterm Exam

ready? date points
true Thu 10/26 09:30AM

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. (9 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 -9 total.)

    Code auto-boxing auto-unboxing

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

    mylist.add(new Integer(9));

    mylist.add(4);

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

    Integer y = mylist.get(0);

    Integer z = mylist.get(0) + mylist.get(1);

  2. (16 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.

    Object Fill in line here
    (a) Alf  
    (b) Bella  
    (c) Charlie  
    (d) Davy  
  3. Suppose the code for the Dog class on the handout were in the file Dog.java and we put the following code in a file called UseDog.java:

    1
    2
    3
    4
    5
    6
    7
    8
    public class UseDog {

    public static void main(String [] args) {
    Dog d1 = new Dog("Alf");
    System.out.println("d1=" + d1);
    }

    }
    1. (10 pts) Assuming that currently only Dog.java and UseDog.java are in your current directory, give the entire sequence of commands you would need to type at a terminal prompt (such as at a terminal shell on CSIL), in order to run the main program from the UseDog class.

    2. (10 pts) When you run the program, the output is this:

      d1=Dog@7852e922
      

      Suppose you want this code to produce the following instead.

      d1=Dog[Alf]
      

      How do you accomplish this by modifying ONLY the source code for Dog.java? Describe briefly what you would have to do, and give the specific source code (i.e. if it’s an additional data member, say what that is—if it’s an additional method or method(s), write those methods.

      (Note: In case it isn’t obvious, Alf should be whatever the particular Dog instance’s name is, not the hard coded value Alf.)

  4. (10 pts) Assume that we have a class Kennel defined as follows:

    public class Kennel extends ArrayList<Dog> {
    ...
    

    Suppose dogs is of type Kennel, and contains an unknown number of dogs. For example, it might contain dogs named Fido, Ralph, and Snoopy.

    Write an instance method called getNames that we could add to the Kennel class that when invoked on an object of type Kennel (e.g. as dogs.getNames() returns an ArrayList<String> value containing precisely the names of each of the Dog objects in the list (e.g. an ArrayList containing `Fido`, `Ralph` and `Snoopy`).

    Hint: Be very careful about the difference between how add and set operate on ArrayList objects. See p. 2 of the Handout with information on java.util.ArrayList for more details.

  5. (10 pts) In Java, we can extend a class, or we can implement an interface.

    Explain the difference (as if you were at a job interview).

    “As if you were at a job interview” means:

    • Your answer should be precise enough that it is clear you know what you are talking about (so you get the job)
    • Your answer should not be so long, tedious or detailed that the interviewer gets bored, annoyed, and decides you’d be a “bad fit” for the company.
  6. (10 pts) In the same “job interview” style, explain the difference between tasks and targets in an Ant build.xml file.

  7. When working with git repositories, we sometimes work with a .git directory, a .gitignore file and a README.md file.

    In the same “job interview” style described earlier, answer these questions:

    1. (5 pts) “We had an intern here once that couldn’t find the .git directory or the .gitignore files in a repo—they kept complaining that they weren’t there when they did a directory listing. Of course you know what they were doing wrong. Why couldn’t they see the files, and what did they need to do in order to see them?

    2. (5 pts) “Of course you know all about git: so tell me what the .git directory in a repo is for.”

    3. (5 pts) “And what about the .gitignore file?

    4. (5 pts) “And the README.md? What does .md even mean, and what is this file for?”

    5. (5 pts) Finally: what’s the difference between doing a git commit and doing a git push?

End of Exam