| 1 |
| e01 |
| CS56 F17 |
![]() |
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.
-
(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
mainMETHOD, 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);□ □ -
(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 -
Suppose the code for the
Dogclass on the handout were in the fileDog.javaand we put the following code in a file calledUseDog.java:1
2
3
4
5
6
7
8public class UseDog {
public static void main(String [] args) {
Dog d1 = new Dog("Alf");
System.out.println("d1=" + d1);
}
}-
(10 pts) Assuming that currently only
Dog.javaandUseDog.javaare 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 theUseDogclass. -
(10 pts) When you run the program, the output is this:
d1=Dog@7852e922Suppose 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,
Alfshould be whatever the particular Dog instance’snameis, not the hard coded valueAlf.)
-
-
(10 pts) Assume that we have a class
Kenneldefined as follows:public class Kennel extends ArrayList<Dog> { ...Suppose
dogsis of typeKennel, and contains an unknown number of dogs. For example, it might contain dogs namedFido,Ralph, andSnoopy.Write an instance method called
getNamesthat we could add to theKennelclass that when invoked on an object of typeKennel(e.g. asdogs.getNames()returns anArrayList<String>value containing precisely the names of each of theDogobjects in the list (e.g. an ArrayListcontaining `Fido`, `Ralph` and `Snoopy`). Hint: Be very careful about the difference between how
addandsetoperate onArrayListobjects. See p. 2 of the Handout with information onjava.util.ArrayListfor more details. -
(10 pts) In Java, we can
extenda class, or we canimplementan 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.
-
(10 pts) In the same “job interview” style, explain the difference between tasks and targets in an Ant
build.xmlfile. -
When working with git repositories, we sometimes work with a
.gitdirectory, a.gitignorefile and aREADME.mdfile.In the same “job interview” style described earlier, answer these questions:
-
(5 pts) “We had an intern here once that couldn’t find the
.gitdirectory or the.gitignorefiles 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? -
(5 pts) “Of course you know all about git: so tell me what the
.gitdirectory in a repo is for.” -
(5 pts) “And what about the
.gitignorefile? -
(5 pts) “And the
README.md? What does.mdeven mean, and what is this file for?” -
(5 pts) Finally: what’s the difference between doing a
git commitand doing agit push?
-
