1
ic02
CS56 M18
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu section
9:30am or 11am
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone"

ic02: Exam prep (garbage collection)

ready? assigned due points
true Wed 08/29 09:30AM Wed 08/29 10:50AM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

MAY ONLY BE TURNED IN IN THE LECTURE/LAB LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the three lowest scores (if you have zeros, those are the three lowest scores.)


https://ucsb-cs56-m18.github.io/hwk/ic02/

  1. (20 pts) Please fill in your name, email and section above.
  2. From W18 Final Exam:

    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("Ace");
    	Dog d2 = new Dog("Buddy");
    	Dog d3 = new Dog("Coco");
    	Dog d4 = new Dog("Daisy");
    	Dog d5 = new Dog("Eddie");
    	Dog d6 = d2;   
    	
    	setBestInShow(d4);       
    	d4 = d1;                 
    	d2 = d3;                 
    	d6 = null;               
    	d5 = null;               
    	d4 = null;               
    	d3 = null;               
    	d2 = null;               
    	d1 = null;               
    	setBestInShow(null);     
        }
    }

    (50 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) Ace  
    (b) Buddy  
    (c) Coco  
    (d) Daisy  
    (e) Eddie  
  3. (10 pts) Given the following program:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    
    import java.util.ArrayList;
    public class BoxUnbox {
        public static void main(String [] args) {
    	ArrayList<Integer> mylist = new ArrayList<Integer>();
    	mylist.add(1);
    	mylist.add(new Integer(4));
    	Integer a = mylist.get(0) + mylist.get(1);
    	int b = mylist.get(0);
    	Integer c = a + b;
    	Integer d = b;
    	Integer e = mylist.get(mylist.get(0));
    	System.out.println("a=" + a + " b= " + b + " c=" + c);
    	System.out.println("d=" + d + " e= " + e);
        }
    }

    What is the output? Indicate by filling in the blanks:

    
    a=____ b= ___ c=____
    
    d=____ e= ____
    
    
  4. (20 pts) With the same program, indicate for each line whether the code involves auto-boxing, auto-unboxing, both or neither. If a line of code involves both, check both boxes. If it involves neither, check neither box.

    Code auto-boxing auto-unboxing

    ArrayList<Integer> mylist = new ArrayList<Integer>();

    mylist.add(1);

    mylist.add(new Integer(4));

    Integer a = mylist.get(0) + mylist.get(1);

    int b = mylist.get(0);

    Integer c = a + b;

    Integer d = b;

    Integer e = mylist.get(mylist.get(0));