1
e01
CS56 w20-mw
PLEASE DO NOT WRITE IN THIS AREA! Name:
UCSB Email address:

EXAM: e01: Midterm 1 MW

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.

  1. Object Fill in line here
    (a) Kiki  
    (b) Logan  
    (c) Max  
    (d) Nicky  
    (e) Olive  

    (10 pts) Refer to the code for the class Dog with a main that creates some Dog objects, found on p.2 of Handout B

    Your 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.

  2. In Java, java.util.TreeMap<K,V> is a class that implements the java.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.

    Map<String,Object> a = new Map<String,Object>(); (3 pts)  Valid   Invalid
    TreeMap<String,Long> c = new TreeMap<String,Long>(); (3 pts)  Valid   Invalid
    TreeMap<String,Object> d = new Map<String,Object>(); (3 pts)  Valid   Invalid
    Map<String,String> b = new TreeMap<String,String>(); (3 pts)  Valid   Invalid
  3. Page 2 of Handout A has some useful reference material for the problems below.

    1. (10 pts) Suppose you have a class Student that does not currently implement the interface Comparable<Student>. This class has a private data member of type int called perm.

      There is no getter for perm. Note that you do not need one to solve this problem, and the solution does not involve writing one.

      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” of Student objects is to be sorted by perm in increasing order.

    2. (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 an ArrayList<Book> instance.

      Write the code to sort books by title, using a built-in sort method of java.util.Collections and a suitable Comparator 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.

  4. 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.
    1. (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 the pom.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.

    2. (6 pts) When using git we work with both branches and remotes. Describe a situation where, as a developer, you’d work with more than one branch.

  5. (3 pts) In the familiar git push origin master, which one is origin?    Check one: A branch     A remote

  6. 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 and Product.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
    1. (3 pts)
        public static void TB40 () {
          Food f40 = new Food(199,"Gummi Bears",520,5);
          System.out.println(f40.getWeight());
        }
      
    2. (3 pts)

        public static void TB41 () {
          Product b41 = new Beverage(89,"Diet Coke",0,12.0);
          System.out.println(b41.getPrice());
        }
      
    3. (3 pts)

        public static void TB42 () {
          Beverage b42 = new Beverage(89,"Diet Coke",0,12.0);
          System.out.println(b42.getFluidOunces());
        }
      
    4. (3 pts)

        public static void TB43 () {
          FreeCandy f43 = new FreeCandy(50);
          System.out.println(f43.getName());
        }
      
  7. 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
    }
    
    1. (3 pts)

        public static void TB44 () {
          Product p44 = new Beverage(199,"Milk",120,6.75);
          System.out.println(p44.getName());
        }
      
    2. (3 pts)

        public static void TB45 () {
          Food f45 = new Food(99,"Peanuts",100,0.63);
          System.out.println(f45.getPrice());
        }
      
    3. (3 pts)

        public static void TB46 () {
          Edible e46 = new Beverage(89,"Diet Coke",0,12.0);
          System.out.println(e46.getCalories());
        }
      
    4. (3 pts)

        public static void TB47 () {
          Product p47 = new Product(299, "Fuzzy Dice");
          System.out.println(p47.getPrice());
        }
      
  8. 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
    }
    
    1. (3 pts)

        public static void TB48 () {
          Product p48 = new Food(249,"Kind Bar",200,1.4);
          System.out.println(p48.getName());
        }
      
    2. (3 pts)

        public static void TB49 () {
          Edible e49 = new Product(299, "Fuzzy Dice");
          System.out.println(e49.getCalories());
        }
      
    3. (3 pts)

        public static void TB50 () {
          Edible e50 = ()->99 ;
          System.out.println(e50.getCalories());
        }
      
    4. (3 pts)

        public static void TB51 () {
          Edible e51 = new Edible() {
            public int getCalories() {
              return 75;
            }
          };
          System.out.println(e51.getCalories());
        }
      
    5. (3 pts)

        public static void TB52 () {
          Product p52 = new FreeCandy(42);
          System.out.println(p52.getName());
        }
      
End of Exam