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
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);
□ □ -
(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
Dog
class on the handout were in the fileDog.java
and 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.java
andUseDog.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 theUseDog
class. -
(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’sname
is, not the hard coded valueAlf
.)
-
-
(10 pts) Assume that we have a class
Kennel
defined as follows:public class Kennel extends ArrayList<Dog> { ...
Suppose
dogs
is of typeKennel
, and contains an unknown number of dogs. For example, it might contain dogs namedFido
,Ralph
, andSnoopy
.Write an instance method called
getNames
that we could add to theKennel
class that when invoked on an object of typeKennel
(e.g. asdogs.getNames()
returns anArrayList<String>
value containing precisely the names of each of theDog
objects in the list (e.g. an ArrayListcontaining `Fido`, `Ralph` and `Snoopy`). Hint: Be very careful about the difference between how
add
andset
operate onArrayList
objects. See p. 2 of the Handout with information onjava.util.ArrayList
for more details. -
(10 pts) In Java, we can
extend
a class, or we canimplement
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.
-
(10 pts) In the same “job interview” style, explain the difference between tasks and targets in an Ant
build.xml
file. -
When working with git repositories, we sometimes work with a
.git
directory, a.gitignore
file and aREADME.md
file.In the same “job interview” style described earlier, answer these questions:
-
(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? -
(5 pts) “Of course you know all about git: so tell me what the
.git
directory in a repo is for.” -
(5 pts) “And what about the
.gitignore
file? -
(5 pts) “And the
README.md
? What does.md
even mean, and what is this file for?” -
(5 pts) Finally: what’s the difference between doing a
git commit
and doing agit push
?
-