1 |
e01 |
CS56 w20 |
Name: | |
---|---|
UCSB Email address: |
EXAM: e01: Midterm 1 TR
ready? | date | points |
---|---|---|
true | Tue 02/18 11:00AM |
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.
-
Object Fill in line here (a) Fido
(b) Ginger
(c) Harry
(d) Izzy
(e) Jack
(10 pts) Refer to the code for the class
Dog
with amain
that creates someDog
objects, found on p.2 of Handout BYour job: figure out after which line of main() each of the following Dog 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.
-
In Java,
java.util.HashMap<K,V>
is a class that implements thejava.util.Map<K,V>
interface. Given those facts, for each of the statements below, check whether it is valid, or invalid.You do not need to explain your answer.
HashMap<String,Long> c = new HashMap<String,Long>();
(3 pts) ☐ Valid ☐ Invalid Map<String,Object> a = new Map<String,Object>();
(3 pts) ☐ Valid ☐ Invalid Map<String,String> b = new HashMap<String,String>();
(3 pts) ☐ Valid ☐ Invalid HashMap<String,Object> d = new Map<String,Object>();
(3 pts) ☐ Valid ☐ Invalid -
Page 2 of Handout A has some useful reference material for the problems below.
-
(10 pts) Suppose you have a class
Student
that does not currently implement the interfaceComparable<Student>
. This class has a private data member of typeint
calledperm
.To make the class implement
Comparable<Student>
, you’d have to change the first line:- from:
public class Student {
- to:
public class Student implements Comparable<Student> {
You’d also need to add one method. Write that method completely, as it would appear inside class
Student
. Assume that the “natural order” ofStudent
objects is to be sorted byperm
in increasing order. - from:
-
(10 pts) Suppose you have a class
Book
that:- does NOT implement
Comparable<Book>
. - has a method
public String getTitle()
Further, suppose that
books
is a reference to anArrayList<Book>
instance.Write the code to sort
books
by title, using a built-insort
method ofjava.util.Collections
and a suitableComparator
implemented as a lambda function.You don’t need to write a complete method or class, just the code needed, assuming that
books
is already declared and instantiated. - does NOT implement
-
-
The questions on this page are “job interview” questions.
In each case, your answer will be graded partially on correctness, and partially on whether the answer would help you get the job. Your answer should be:
- Not too long, and rambling. That annoys the interviewer and wastes time.
- Not too short. It’s too short if it leaves out so much detail that the the interviewer isn’t sure whether you really understand the concept.
-
(10 pts) The interviewer asks:
I see from your resume that you’ve worked with Maven. Please tell me:
- What’s the purpose of the
<dependencies>
section of thepom.xml
file, and - When would you need to add something to that section?
Give an answer that covers both parts of the interviewer’s question.
- What’s the purpose of the
-
(6 pts) When using
git
we work with both branches and remotes. Describe a situation where, as a developer, you’d work with two different remotes.
-
(3 pts) In the familiar
git push origin master
, which one ismaster
? Check one: ☐ A branch ☐ A remote -
For this question, you need page 1 of Handout A and page 1 of Handout B.
There, you will find code for these files:
Beverage.java
,Edible.java
,Food.java
,FreeCandy.java
andProduct.java
. These are classes used by a grocery store known as “Trader Bobs”.Some of these methods will compile and run, while others will not.
Indicate, for each method, whether it compiles or not, in context of the code on page 1 of Handout A and page 1 of Handout B and assuming the methods appear inside this class:
public class TraderBobs { // methods appear here }
You do not need to indicate the output, or the reason; only whether it does, or does not compile.
Will it compile? ☐ Yes
☐ No- (3 pts)
public static void TB20 () { Product p20 = new Product(299, "Fuzzy Dice"); System.out.println(p20.getPrice()); }
-
(3 pts)
public static void TB21 () { Product p21 = new Food(249,"Kind Bar",200,1.4); System.out.println(p21.getName()); }
-
(3 pts)
public static void TB22 () { Edible e22 = new Product(299, "Fuzzy Dice"); System.out.println(e22.getCalories()); }
-
(3 pts)
public static void TB23 () { Edible e23 = ()->99 ; System.out.println(e23.getCalories()); }
- (3 pts)
-
Continued from previous problem…
Some of these methods will compile and run, while others will not.
Indicate, for each method, whether it compiles or not in context of the code on page 1 of Handout A and page 1 of Handout B and assuming the methods appear inside this class:
public class TraderBobs { // methods appear here }
-
(3 pts)
public static void TB24 () { Edible e24 = new Edible() { public int getCalories() { return 75; } }; System.out.println(e24.getCalories()); }
-
(3 pts)
public static void TB25 () { Food f25 = new Food(199,"Gummi Bears",520,5); System.out.println(f25.getWeight()); }
-
(3 pts)
public static void TB26 () { FreeCandy f26 = new FreeCandy(50); System.out.println(f26.getName()); }
-
(3 pts)
public static void TB27 () { Product p27 = new Beverage(199,"Milk",120,6.75); System.out.println(p27.getName()); }
-
-
Continued from previous problem…
Some of these methods will compile and run, while others will not.
Indicate, for each method, whether it compiles or not, in context of the code on page 1 of Handout A and page 1 of Handout B and assuming the methods appear inside this class:
public class TraderBobs { // methods appear here }
-
(3 pts)
public static void TB28 () { Food f28 = new Food(99,"Peanuts",100,0.63); System.out.println(f28.getPrice()); }
-
(3 pts)
public static void TB29 () { Edible e29 = new Beverage(89,"Diet Coke",0,12.0); System.out.println(e29.getCalories()); }
-
(3 pts)
public static void TB30 () { Product p30 = new FreeCandy(42); System.out.println(p30.getName()); }
-
(3 pts)
public static void TB31 () { Beverage b31 = new Beverage(89,"Diet Coke",0,12.0); System.out.println(b31.getFluidOunces()); }
-
(3 pts)
public static void TB32 () { Product b32 = new Beverage(89,"Diet Coke",0,12.0); System.out.println(b32.getPrice()); }
-