CS56—Midterm Exam 2
E02, S11, Phill Conrad, UC Santa Barbara
Tuesday, 05/24/2011

Name: ___________________________________________________


Umail Address: ______________________________@ umail.ucsb.edu



Discussion section (Circle one:)       6:30pm      7:30pm     



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:


  1. (30 pts) We've discussed each of the following "things" that can occupy memory in Java
    • primitive values
    • references
    • object instances

    We've also discussed two areas in memory:

    • the stack
    • the heap

    In the table on the next page, there is a row for each "thing" that can be in memory. There is a column for stack, and a column for heap. There is also a space for you to fill in some code in a Java class with a main program.

    For each of the entries in the table, consider whether that "thing" can end up in that area of memory (either stack or heap).

    • If it can happen, add some code to the class that illustrates this, write a brief description of the circumstances under which that "thing" ends up in that area of memory, making specific reference to some part of the code you wrote.
    • If it cannot happen, please write IMPOSSIBLE and explain why this cannot happen in Java.







  2. (5 pts) What do you do if you want to make it impossible for a Java class to be extended (i.e. make it so that no other class can inherit from the class you are writing?)







  3. True or False—answer each question as it applies to the Java language only.

     
    Circle one:
    (2 pts) A primitive variable can be nested inside an object instance
    True
    False
    (2 pts) A reference variable can be nested inside an object instance
    True
    False
    (2 pts) An object instance can be nested inside another object instance
    True
    False
    (2 pts) A reference may refer to a primitive variable
    True
    False
    (2 pts) A reference may refer to another reference
    True
    False
    (2 pts) A reference may refer to an object instance
    True
    False

    (See instructions for problem 1 on previous page)

                
    
    public class Example {        
    
    
    
    
    
    
    
    
    
    
    
                
    
     
    
    
    
    
    
    
    
     
     
          
                                                      
    
    
     
     
    
    
    
     
     
     
     
     
     
     
    
    
    
    
    } // end class                  
          
          

     

    heap      
    stack      
      primitive reference object
    instance
  4. Consider an adventure game in which players in the game have goals that they must achieve to earn points (e.g. climb a mountain, build a shelter, etc.

    They may need certain tools to achieve those goals. So, there will be a variety of classes that represent the various tools.

    Tools can be bought and sold, so there will need to be methods for buying and selling that work on any tool such as public int getPrice(); and as such, there needs to be some generic type for tools.

    The players also have to consume food and beverages in order to stay healthy. The game models the fact that different types of food provide different nutrition, and so there are a variety of types of food and drink with different effects on the players' health. So, there will be a variety of classes to represent the various types of food and drink.

    Since a character needs to be able to consume a food or drink item, so there needs to be a method in the Player class such as
    public void consume( _____ item); where the blank needs to be filled in with some kind of type that represents a food or drink in general.

    Players have to carry around tools, food and beverages in their backpack. The backpack can only hold a certain amount of "stuff" before it gets full. So, in the Backpack class, there needs to be a a method:

      public void addToPack(_______ thing) throws WontFitInPackException;

    where the blank needs to be filled in with some kind of type that represents a thing that can be added to a backpack.

    Now, these questions:

    1. (10 pts) Should the generic type for a tool be a regular class, abstract class, or an interface?

      Make a choice, justify your answer, and write the first line of Java code of that class, abstract class, or interface
      (up to the opening brace). This includes choosing an appropriate name for the class or interface.


















    2. (10 pts) Should the generic type for a food or drink be a regular class, abstract class, or an interface?

      Make a choice, justify your answer, and write the first line of Java code of that class, abstract class, or interface
      (up to the opening brace). This includes choosing an appropriate name for the class or interface.











      continued from previous question...


    3. (10 pts) Should the generic type for an item that can go in a backpack be a regular class, an abstract class or an interface?

      Make a choice, justify your answer, and write the first line of Java code of that class, abstract class, or interface
      (up to the opening brace). This includes choosing an appropriate name for the class or interface.

























    4. (5 pts) Write the Java code that defines a new type of user defined exception called WontFitInPackException that can be thrown whenever a player tries to add something to a backpack, but that new item won't fit.






















  5. (8 pts) Write a few lines of java code that illustrate both auto-boxing and auto-unboxing.

    For full credit:

    • CLEARLY LABEL the line that does auto-boxing with a comment // auto-boxing
    • CLEARLY LABEL the line that does auto-boxing with a comment // auto-unboxing




















  6. (10 pts) Describe what a layout manager does in Swing.

    In your answer include at least two specific examples of layout managers and describe the difference between them.


























End of Exam

Total points: ?