1
e01
CS56 f18
DO NOT WRITE IN THIS AREA! Name: Seat:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu

EXAM: e01: Midterm 1

ready? date points
true Tue 10/23 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.

  1. Suppose you write a Student class in Java with private data members:

     private String name;
     private int perm;
    

    and constructor:

    public Student(String name, int perm);
    

    In a main program, you write:

    1
    2
    
      Student s = new Student("Chris Gaucho",1234567);
      System.out.println("s=" + s);

    The output is:

    s=Student@7852e922
    
    1. (3 pts) What method is being implicitly invoked on the object s on line 2 of the listing above to produce the value Student@7852e922, and in what class is that method defined?

    2. (3 pts) Describe what you would need to do if you wanted to change the output of line 2 above to something like
      s=Chris Gaucho(1234567) but WITHOUT changing line 2.

      Your change should make it so that any time an reference to a Student object is concatenated to something like "x=", this formatting of the name and perm is automatically done.

      Note: Don’t write the Java code, just describe in English, using correct Java and Object-Oriented Programming terminology, at a high-level, what code needs to be written, and in what class that code belongs.

  2. For this question, you need page 1 of Handout A with code for various Java files: BeachCruiser.java, Electric.java, Scooter.java, Bicycle.java, EBike.java, and Rentable.java. This code is in a program called GetAround that helps users plan how to get around Isla Vista.

    All of the files on Handout A are complete and will compile EXCEPT for EBike.java, which is missing two things:

    • The body of the constructor is empty, and needs some code (lines 18-20)
    • There is one other thing missing (indicated by “something missing here” on line 10).
    1. (8 pts) Fill in the missing code for the constructor in the space below. The correct answer is somewhere between two and six lines of code, and should fit easily in the space provided. (Hint: Pay attention to inheritance and interfaces.)

       public EBike( int numGears,
                       double wheelDiameter,
                       String name,
                       String batteryType) {
      
      
      
      
      
      
         }
      
    2. (4 pts) In the space below, write the other missing part from EBike.java It is at least one line of code, and at most three lines of code; so it should easily fit in the space provided. Answer as though the code you write would be placed at line 10 of the listing for EBike.java on Handout A.

      
      
      
      
      
  3. Continue to refer to the code on Handout A.

    Please assume that you have made the changes from the two questions above.

    Indicate, for each method, whether it compiles or not, and if it does compile, theoutput when invoked.

    Will it compile?Output when called (only if it compiles)
    Yes
    No
    1. (3 pts)
        public static void ga01() {
          BeachCruiser a = new BeachCruiser(21.5, "blue");
          System.out.println("ga01: " +  a.getColor());
        }
      
    2. (3 pts)

        public static void ga02() {
            Bicycle b = new BeachCruiser(22.0, "yellow");
            // System.out.println("ga02: " +  b.getColor());
        }
      
  4. 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 the context of the classes on Handout A.

    1. (3 pts)

        public static void ga03() {
            BeachCruiser c = new BeachCruiser(26.0, "red");
            System.out.println("ga03: " +  c.getWheelDiameter());
        }
      
    2. (3 pts)

        public static void ga04() {
            Rentable d = new EBike(1,24.0,"Hopr","12V");
            System.out.println("ga04: " + d.getCost(5,45));
        }
      
    3. (3 pts)

        public static void ga05() {
            Rentable e = new EBike(1,24.0,"Hopr","12V");
            // System.out.println("ga05: " + e.getName());
        }
      
    4. (3 pts)

        public static void ga06() {
            Electric f = new EBike(3,26.0,"Hopr3","12V");
            // System.out.println("ga06: " + f.getName());
        }
      
  5. 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 the context of the classes on Handout A.

    1. (3 pts)

        public static void ga07() {
            Electric g = new Scooter("Lime");
            System.out.println("ga07: " + g.getBatteryType());
        }
      
    2. (3 pts)

        public static void ga08() {
            Scooter h = new Scooter("Bird");
            System.out.println("ga08: " + h.getChargeTime());
        }
      
  6. (10 pts) Refer to the code for the class Dog with a main that creates some Dog objects.

    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.

    Object Fill in line here
    (a) Kiki  
    (b) Lassie  
    (c) Molly  
    (d) Ninja  
    (e) Otto  
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    
    public class Dog {
        
        private static Dog bestInShow = null;
        private String name;
        
        public static void setBestInShow(Dog b) {
            bestInShow = b;
        }
        
        public static Dog getBestInShow() {
            return bestInShow;
        }
        
        public Dog(String name) { this.name = name;}
        
        public static void main(String [] args) {
    
            Dog d1 = new Dog("Kiki");
            Dog d2 = new Dog("Lassie");
            Dog d3 = new Dog("Molly");
            Dog d4 = new Dog("Ninja");
            Dog d5 = new Dog("Otto");
            Dog d6 = d3;
            
            setBestInShow(d2);   
            d5 = d1;             
            d4 = d2;             
            d1 = null;           
            d3 = null;           
            d5 = null;           
            d2 = null;           
            d4 = null;           
            d6 = null;           
            setBestInShow(null); 
        }
    }
  7. Given the following program below, determine whether each line of code involves boxing or unboxing, and check either neither box, one of the boxes, or both boxes, as applicable.

    There is a reference for ArrayList on p. 2 of Handout A if that helps.

    Points Line
    number
    Auto-Boxing Auto-Unboxing
    (2 pts) 5
    (2 pts) 6
    (2 pts) 7
    (2 pts) 8
    (2 pts) 9
    Points Line
    number
    Auto-Boxing Auto-Unboxing
    (2 pts) 10
    (2 pts) 11
    (2 pts) 12
    (2 pts) 13
    (2 pts) 14
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
    import java.util.ArrayList;
    public class BoxUnbox {
      public static void main(String [] args) {
         ArrayList<Integer> mylist = new ArrayList<Integer>();
         mylist.add(3);
         mylist.add(new Integer(2));
         Integer a = mylist.get(0);
         int b = mylist.get(1);
         mylist.add(a);
         mylist.add(b);
         mylist.add(a+1);                
         mylist.set(mylist.get(1),7);
         int c = mylist.get(2);
         Integer d = mylist.get(mylist.get(0));
         System.out.println("a=" + a + " b= " + b + " c=" + c + " d=" + d);
         System.out.println(mylist.size());
      }
    }
  8. Working with the same program as in the previous problem, what is the output of:

    (8 pts) Line 15? (fill in blanks below) (4 pts) Line 16? (fill in box below)
    a=____ b=____ c=____ d=____
  9. Suppose you were asked the two questions below at a job interview. How would you answer?

    For full credit:

    • Focus on what the interviewer is asking. In each case, they are asking “What problem does the tool solve, or what need does it address”. Be sure you answer that question.
    • Be sure there is enough detail in your answer that the interviewer is sure you understand what you are talking about.
    • On the other hand, avoid including unnecessary detail. No one likes a long-winded answer, and if you accidentally say something incorrect, you may lose points (and in the real world, the opportunity for a job offer.)
    1. (4 pts) Ant and Maven are both tools that are used to solve the same basic problem. What is the problem that these tools are designed to solve, or the need that they address? Be sure to identify what kind of person has that problem or need, and in what circumstances.
    2. (4 pts) JUnit solves a different problem (or addresses a different need). What problem does JUnit solve (or what need does it address? As before, identify who has the problem/need and under what circumstances.
  10. Suppose you were asked the two questions below at a job interview. How would you answer?

    For full credit:

    • Focus on what the interviewer is asking. In each case, they are asking “what is the problem that the tool solves?” Be sure you answer that question.
    • Be sure there is enough detail in your answer that the interviewer is sure you understand what you are talking about.
    • On the other hand, avoid including unnecessary detail. No one likes a long-winded answer, and if you accidentally say something incorrect, you may lose points (and in the real world, the opportunity for a job offer.)
    1. (4 pts) What problem does Jacoco solve, or what need does it address? Who has that need or problem, and in what context?
    2. (4 pts) Same question, about Javadoc
End of Exam