1 |
e01 |
CS56 F16 |
Name: | |
---|---|
(as it would appear on official course roster) | |
Umail address: | @umail.ucsb.edu |
EXAM: e01: Midterm Exam
ready? | date | points |
---|---|---|
true | Tue 10/18 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.
-
(10 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) Fido
(b) Princess
(c) Rover
(d) Snoopy
(e) Spot
-
(5 pts) On the handout you got for this exam, there is a
Dog
class. TheDog
class, as written, lacks an overriddentoString
method.Write the code for an overridden
toString
method the returns the Dog’s name. -
(10 pts) Assume that in a given program, there might be a variable
names
that is of typeArrayList<String>
, and contains an unknown number of dog names. For example, it might containFido
,Ralph
, andSnoopy
.Write a static method that we could add to the
Dog
class that takes such a variable as its parameter, and returns anArrayList<Dog>
containingDog
object with precisely those names.Hint: Be very careful about the difference between how
add
andset
operate onArrayList
objects. -
(10 pts) Suppose some constructor method is declared in its Javadoc to be “known to throw” some kind of checked exception—for example,
public FileReader(File file) throws FileNotFoundException
And you are writing a line of code such as this inside some method.
BufferedReader br = new BufferedReader(new FileReader(new File("file.txt")));
Suppose your method is:
public static void foo() { ... }
When you see that the javadoc says
throws FileNotFoundException
and you lookup and discover thatFileNotFoundException
is a checked exception, as programmer, you now have two choices. Briefly explain the two choices. -
(5 pts) We are using a third-party library called JUnit in this course. If you were asked at a job interview to briefly describe the purpose of
JUnit
, 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.
-
(5 pts) Same question, but this time
ant
and thebuild.xml
file.If you were asked at a job interview to briefly describe the purpose of
ant
and thebuidl.xmlbuild.xml file, what would you say? Same comment as above about the “level of detail”. -
On the handout you got for this exam, there is a
Dog
class. TheDog
class, as written, lacks an overridden.equals
method.Yet, it is still possible to invoke
.equals()
on Dog objects.Briefly, but preicsely, answer these questions.
-
(5 pts) Write two lines of code that declare two variables
a
andb
, each of which is a Dog reference, and initialize both to separate Dog objects with the nameAlice
. -
(5 pts) Write a few lines of java that compare the objects referred to by
a
andb
using the.equals
method, and then print “EQUAL” on the standard output stream if the method returns true. -
(10 pts) Briefly explain how the
.equals()
method invoked in this case (with no overloaded.equals
method inDog
) determines whether to returntrue
orfalse
, both in this specific case, and in general. (Both parts needed for full credit). -
(5 pts) Continuing with the
Dog
class: if/when you override the.equals()
method for a class, it is considered a “Java best practice” to always override at least one other method—In below, write the “signature” for that method, i.e. the return type, name of method, and the parameters it takes if any (the same way it appears in Javadoc, for example.)
Hint: if you are drawing a blank, check the method list for
java.util.ArrayList<E>
, because its on the list. That turns this into a “multiple choice question, albeit one with 42 choices.
-
-
(10 pts) Continuing with the
Dog
class from the handout: handoutIn the space below, please write a properly written .equals() method for Dog.
The reverse side of the
ArrayList
handout has some reminders about a properly written.equals()
method. -
(10 pts) Continuing with the
Dog
class from the handout: handoutIn the space below, please write the full code for the other method you need to override when ever you override .equals(). (Hint: consider that java.lang.String already has this method.)
-
(10 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: -2 for each incorrect answer, but no more than -10 total.)
Code auto-boxing auto-unboxing ArrayList<Integer> mylist = new ArrayList<Integer>();
□ □ mylist.add(9);
□ □ mylist.add(new Integer(3));
□ □ mylist.add(mylist.get(0) + 1);
□ □ int x = mylist.get(0);
□ □ Integer y = mylist.get(1);
□ □