1 |
e01 |
CS56 f19 |
Name: | |
---|---|
UCSB Email address: |
EXAM: e01: Midterm 1
ready? | date | points |
---|---|---|
true | Mon 10/28 05:00PM |
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.
-
As a reminder, in Java,
java.util.ArrayList
is a class that implements thejava.util.List
interface. Given those facts, for each of the statements below, check whether it is valid, or invalid, and when you mark invalid, explain why.List<String> a = new List<String>();
(3 pts) ☐ Valid ☐ Invalid List<String> b = new ArrayList<String>();
(3 pts) ☐ Valid ☐ Invalid ArrayList<String> c = new List<String>();
(3 pts) ☐ Valid ☐ Invalid ArrayList<String> d = new ArrayList<String>();
(3 pts) ☐ Valid ☐ Invalid -
Throughout this exam, there will be questions of the form:
Suppose you are at a job interview and the interviewer asks you about
_______
. How do you respond?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.
This time, the interviewer says this:
One of the goals of the team you’ll be working on is to increase the test coverage of our product. Do you know what test coverage is, and if so, can you explain it briefly?
Give an answer that:
- (6 pts) Explains briefly what test coverage is
- (4 pts) Mentions, specifically, by name, what tool you used in CS56 to measure test coverage in Java programs (e.g. in lab03 and lab05).
-
(10 pts) For this problem, you may find it helpful to consult the reference material regarding interfaces and methods related to sorting on page 2 of Handout A, and the information about
ArrayList
on page 2 of Handout B.- Below, you will code for the file
MenuItem.java
. -
Alter this code so that an
ArrayList<MenuItem>
calledmenu
can be sorted byname
using this line of code:java.util.Collections.sort(menu);
Make the two changes needed, directly on the listing below.
You may cross out code, and/or write new code where it belongs.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
public class MenuItem { private String name; private int priceInCents; private String category; public MenuItem(String name, int priceInCents, String category) { this.name = name; this.priceInCents = priceInCents; this.category = category; } public String getName() { return this.name;} }
- Below, you will code for the file
-
(10 pts) Job interview question:
In C++, if you write a class that allocates memory on the heap at any point in the lifetime of an object, you typically have to write a destructor that returns that memory to the heap.
Is it the same or different in Java? Explain your answer.
Remember:
- 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 or are just repeating buzzwords that you’ve memorized.
-
Two part job interview question:
When using
git
we typically have to do both agit commit
and agit push
.-
(5 pts) What is happening at the
git commit
stage that isn’t happening at thegit push
stage (other than the fact that we add a commit message at this stage.) -
(5 pts) What is happening at the
git push
stage that isn’t happening at thegit commit
stage?
-
-
For this question, you need page 1 of handout A and page 1 of handout A.
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, and if it does compile, the output when invoked. 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 }
WARNING: Be precise with your output.
If there is a label such as
"p1: "
, include it in the output, or you may lose points.Will it compile? Output when called (only if it compiles) ☐ Yes
☐ No- (4 pts)
public static void TB01 () { Product p1 = new Beverage(99,"Coke",150,12.0); System.out.println("p1: " + p1.getName()); }
-
(4 pts)
public static void TB02 () { Edible e2 = new Food(249,"Kind Bar",200,1.4); System.out.println("e2: " + e2.getName()); }
-
(4 pts)
public static void TB03 () { Edible e03 = ()->42 ; System.out.println("e03: " + e03.getCalories()); }
-
(4 pts)
public static void TB04 () { Food f4 = new Food(199,"Gummi Bears",520,5); System.out.println("f4: " + f4.getPrice()); }
- (4 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, and if it does compile, the output when invoked. 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 }
-
(4 pts)
public static void TB05 () { FreeCandy f5 = new FreeCandy(50); System.out.println("f5: " + f5.getCalories()); }
-
(4 pts)
public static void TB06 () { Food f6 = new Food(99,"Peanuts",100,0.63); System.out.println("f6: " + f6.getName()); }
-
(4 pts)
public static void TB07 () { Beverage b7 = new Beverage(89,"Diet Coke",0,12.0); System.out.println("b7: " + b7.getCalories()); }
-
(4 pts)
public static void TB08 () { Product p8 = new Beverage(199,"Milk",120,6.75); System.out.println("p8: " + p8.getCalories()); }
-
-
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, and if it does compile, the output when invoked. 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 }
-
(4 pts)
public static void TB09 () { Edible e9 = new FreeCandy(42); System.out.println("e9: " + e9.getCalories()); }
-
(4 pts)
public static void TB10 () { Edible e10 = new Edible() { public int getCalories() { return 100; } }; System.out.println("e10: " + e10.getCalories()); }
-
(4 pts)
public static void TB11 () { Product p11 = new Product(299,"Ziploc Bags"); System.out.println("p11: " + p11.getPrice()); }
-
(4 pts)
public static void TB12 () { Food f12 = new Food(99,"Peanuts",100,0.63); System.out.println("f12: " + f12.getFluidOunces()); }
-