CS56—Midterm Exam 2
E02, W12, Phill Conrad, UC Santa Barbara
Thursday, 03/01/2012

Name: ___________________________________________________


Umail Address: ______________________________@ umail.ucsb.edu



Discussion section (Circle one:)       2pm    3pm    9am     



Please write your name only on this page.
That allows me to grade your exams without knowing whose exam I am grading.

This exam is closed book, closed notes, closed mouth, cell phone off,
except for:

There are 100 points worth of questions on the exam, and you have 75 minutes to complete the exam.

A hint for allocating your time—on your first pass through the exam:

If you do that, after you complete your first pass through the exam in 50 minutes, you'll still have 25 minutes to:


IMPORTANT: Unless otherwise stated, all of the questions on this page pertain to programming in Java.
Do not consider any other programming language in your answer, unless the questions specifically calls for it.

  1. (5 pts) Under what circumstances can a variable of a primitive type take up space on the stack?
    (If never, write "never").





  2. (5 pts) Under what circumstances can a variable of a primitive type take up space on the heap?
    (If never, write "never").






  3. (5 pts) Under what circumstances can a variable of a reference type take up space on the stack?
    (If never, write "never").
    (Note: this question refers to the reference variable itself, not the object referred to.)






  4. (5 pts) Under what circumstances can a variable of a reference type take up space on the heap?
    (If never, write "never").
    (Note: this question refers to the reference variable itself, not the object referred to.)






  5. (5 pts) Under what circumstances can a reference refer to (i.e. "point to") an object on the stack?
    (If never, write "never").






  6. (5 pts) Under what circumstances can a reference refer to (i.e. "point to") an object on the heap?
    (If never, write "never").




  7. (5 pts) Consider the following code excerpt:
    public final class Foo {
    ... // code continues from here
    

    In this context, what does the word final signify about the class Foo?


  8. Suppose you want to create a new type of user-defined exception, a checked exception, called GauchoCodeViolationException


    1. (5 pts) Write the minimum amount of code that would accomplish this.













    2. (5 pts) What file would you put this code in?





  9. (5 pts) Briefly, what is the purpose of a layout manager?
    Please be as precise as you can, using specific Java terminology.







  10. (5 pts) Briefly, under what circumstances would you want to make a class
    implement the Serializable interface (more specifically, java.io.Serializable)?








  11. Suppose a class implements the ActionListener interface
    (more specifically, java.awt.event.ActionListener)

    1. (5 pts) What is the purpose of having an instance of such a class—i.e. in what context would a class that implements ActionListener be useful?







    2. (5 pts) Objects that implement ActionListener often end up being "inner classes".
      What kinds of objects are typically the "outer classes" in this circumstance?







    3. (5 pts) Why is this "inner class/outer class" relationship a frequently used design—i.e. what is the advantage of making an ActionListener be an "inner class"—as opposed to not doing that?







  12. For this question, you need the handout that came with this exam—a handout with code for these files:
    Book.java, Product.java, Shippable.java, Song.java

    You may assume that all of the code on the handout compiles—I've checked that this is true.
    Now consider the following code, which does contain some errors, and as a result, will NOT compile.
    1public class Q1 {
    2 public static void main (String [] args) {
    3
    4 Book gp = new Book("Pratchett","Going Postal",799,0.15);
    5 Song br = new Song("Lady Gaga","Bad Romance");
    6 Product slts = new Song("Nirvana","Smells Like Teen Spirit",79);
    7 Shippable hp = new Book("Rowling","Harry Potter & the Polymorphic Polyp",652,1.5);
    8 Shippable ttc = new Shippable("Dickens","Tale of Two Cities",999,1.5);
    9
    10 System.out.println("a:" + gp.getTitle());
    11 System.out.println("b:" + br.getArtist());
    12 System.out.println("c:" + br.getPrice());
    13 System.out.println("d:" + slts.getPrice());
    14 System.out.println("e:" + slts.getTitle());
    15 System.out.println("f:" + hp.getPrice());
    16 System.out.println("g:" + hp.getWeight());
    17 System.out.println("h:" + hp.getTitle());
    18 System.out.println("i:" + ttc.getPrice());
    19 System.out.println("j:" + ttc.getPrice());
    20
    21 } // main method
    22} // class Q1
    Please do things with this "broken" code for the Q1 class:

    1. (15 pts) Several lines need to be eliminated from this file in order to make it compile.
      Find the lines that are bogus, and draw a line through each of them.

      Hint: By "several", I mean more than 2, and fewer than 10.
      Start by determining which, if any, of the constructors are bogus.
      Then, eliminate any lines that refer to the variables created on those lines.
      Finally, check all of the remaining method calls.

      You will lose points for striking lines that are notbogus, and you will lose points for failing to strike lines that arebogus. So, choose wisely.
    2. (15 pts) After striking through the bogus lines, the remaining code should compile and run. So, indicate what the output will be (if any) below. Be precise. If there will no output, write "no output".

End of Exam

Total points: ?

Handout for E02, CS56, W12

Product.java

1/** something that can be sold */
2public abstract class Product {
3
4 /** get the price (in cents) */
5 public abstract int getPrice();
6
7}

Shippable.java

1/** something that can be shipped */
2public interface Shippable {
3
4 /** get the shipping weight in pounds */
5 public double getWeight();
6}

Book.java

1/** A Book */
2public class Book extends Product implements Shippable {
3
4 private int price;
5 private double weight;
6 private String author;
7 private String title;
8
9 public Book(String author, String title, int price,
10 double weight) {
11 this.author = author;
12 this.title = title;
13 this.price = price;
14 this.weight = weight;
15 }
16
17 public int getPrice() {return this.price;}
18 public String getTitle() {return this.title;}
19 public String getAuthor() {return this.author;}
20 public double getWeight() {return this.weight;}
21
22}

Song.java

1/** A downloadable Song */
2public class Song extends Product {
3
4 private int price;
5 private String artist;
6 private String title;
7
8 public Song(String artist, String title, int price) {
9 this.artist = artist;
10 this.title = title;
11 this.price = price;
12 }
13
14 public Song(String artist, String title) {
15 this(artist,title,99);
16 }
17
18 public int getPrice() {return this.price;}
19 public String getTitle() {return this.title;}
20 public String getArtist() {return this.artist;}
21
22}