CIT 591 Midterm Exam - Fall, 2006
Name _________________________

1.        i. ( 5 points )What are the 5 XP values ?

    Simplicity: Keep it simple
    Feedback: Use various tests
    Courage: Be willing to throw away code
    Respect: nTrust your team members not to submit non working code
    Communication: Talk with your fellow programmers and customers

ii. ( 3 points )What is a namespace and how many kinds are there ?

    nA namespace is a place in which Java keeps track of names
    Java uses six different namespaces

2.  ( 3 points )                                                                                                            

i)                    By default, what is the type of 2.47 ?    double 

ii)                   When should float be used instead of double ?

           When there are lots of numbers and space is limited.

iii)                 What is the problem with the test

    if ( pi = = 3.14 ) { }

    Since doubles are not stored exactly as their actual mathematical values, the above comparison is unreliable.       

3.     i )     ( 3 points )  An integer ‘x’, has a binary value of 0…010011100.    What is the binary value of z after these statements:                                                                                                                        

int y = 1 << 7;
int z = x & y;

     z = 0...0 10000000

     ii )  ( 3 points ) What is the output of the code                                                        

        int i = 0;
    i = i++;
    System.out.println(i++);
 0 

   Reason: In the second statement 'i' is incremented but again assigned a value 0,  and then it is printed before being incremented.

4.   (8 points)    Given the following variables :                                         

    int one = 1;
    int two = 2;
    char initial = '2';
    boolean flag = true;

Which of the following are valid?

if( one ){}
switch( one ){}          VALID
if( one = two ){}
switch( flag ){}
if( one == two ){}        VALID
switch( initial ){}        VALID
if( flag ){}                   VALID
if(one.equals(two) ) { }

 
5.                   i)   (2 points) What is the problem with the following code ?                                  
    String capital = "";
    String state = "Colorado";

 switch (state) {
    case " Georgia ":
        capital = " Atlanta" ;
        break ;
    case "Massachusetts":
        capital = “ Boston" ;
        break ;

    default :
        System.out.println("That is all I know");
        break ;
  }

           We can't use String in a switch statement. 

ii)            (2 points)        Given that jobQueue  is an object of ArrayList<Job>, What will happen if the list is not empty and the following code is executed ?             

		assert jobQueue.size() == 0 : "process: queue must be empty";

    If asserts are enabled then AssertionError is thrown with the given message string.

6.                   i)  (3 points)  Class Animal has a instance variable name and an instance
	 method play(). Class Dog extends Animal, and has instance methods play() and bark().                                                         

        Animal myPet = new Dog();

        myPet.play( );

            Will the above code compile ? If yes, which play() is really called, the one in Animal, or the one in Dog? If not, why not?

            Yes, it will compile and Dog's play method will be called.

ii)   (6 points)  Given :                                                                                    

    String [][] crapBag = {  { "I like java with bagel !" }  };

    Write one line of code that will give the output

    I like java with bagel !

    System.out.println(crapBag[0][0]);    

    Alternatively, System.out.println("I like java with bagel !");



7. (20 points) Give short precise answers                                                       

i)                    Which methods can change a String object ?

            None, String Objects are immutable

ii)                   Which package is automatically imported into every Java program?

            java.lang

iii)                 Abstract classes enable you to force all the subclasses to write certain methods. Is there a way to stop the subclasses from writing certain methods ? If yes, how ?

           Yes we can do that by making the methods final.

iv)                 Interface methods are implicitly   public   and    abstract    .

v)                  Interface fields are implicitly    public     static      and      final   .

vi)                 What is an Adapter class ?

            An adapter class implements an interface/abstract class and provides empty method bodies. Typically used to reduce the code for event listeners.

vii)               A class can implement   any    (number) interfaces and it can extend     only one      (number) class(es).

viii)              There are   4    kinds of inner classes. Two of these are practically useful; name those two.

               i) Anonymous  ii)  Member

ix)                 What is the difference between overloaded and overridden methods in terms of arguments and return types.

            In overridden methods, signature has to be same while in overloaded methods the signature has to be different. Note, the return type is not a part of the unique signature.

x)                  What is the difference between what you should talk about in your javadoc comments and in your internal comments ?

           Javadoc defines the contract and has nothing to do with the internal code while internal comments describe the code and other internal details.

8. ( 4 points )How many objects are eligible for garbage collection once execution has reached the line labeled Line A?                                                                                                         

Person student;
Person newStudent = new Person("Nick");
newStudent = new Person("Dave");
student = new Person("Anita");
Person newestStudent = new Person("Chris");
student = null;
//Line A

         2, Nick and Anita

9. ( 7 points ) If standard Java style rules are followed then indicate which of the following could be

       classes, interfaces, methods, variables, constants                                               

a) variable - methods & variables

b) Variable - classes &  interfaces

c) VARIABLE - constants 

9. ( 8 points ) Fix the constructor so the field's value is set                                       

class Person{
     public String name;

     Person(String name){
        this.name = name;
     }

     public boolean equals(Object o){
         if(o == null || !(o instanceof Person))

       return false;
       Person p = (Person) o;
       return "John".equals(o.name);
    }

}

Assuming the constructor is fixed which of the following will be true?

Person john = new Person("John");
Person kate = new Person("Kate");

1. john == kate      False

2. john.equals(kate) False

3. kate.equals(john) True
    

10. ( 5 points ) Write whether you should use inheritance or composition in the following scenarios:

Car – SoundSystem   :                        Composition             

Cow – Animal            :                      Inheritance                

Jet – Plane                 :                      Inheritance                

Plane – Vehicle         :                       Inheritance                

Room – Wall             :                      Composition             

11. ( 2 points each )                                                                                                                     

i)  If I have a String in my program that I will make thousands of changes on, I should use a            StringBuffer/StringBuilder      

ii) You can use    Generics   to ensure that your ArrayLists contain only the types of objects you specified.

 

12. ( 6 points )What two lines are printed by the following code:                                                      

class Dog {
    public int numberOfFleas;
    public String name;
    Dog(String name) {
        this.name = name;
    }
}

class PetShop {
    static void changeDogName(Dog d) {
        d.name = "Fido";
        d = new Dog("Joey");
        d.name = "Jim";
        System.out.println(d.name);
    }

    public static void main(String[] args) {
        Dog doggie = new Dog("Marty");
        changeDogName(doggie);
        System.out.println(doggie.name);
    }
}
	Jim
	Fido
 
13. ( 8 points )State True or False                                                                                        
a)         True    A test suite can contain calls to other test suites 
b)         True    Declaring a class abstract prevents it from being instantiated.
c)         False     Interfaces can have static methods
d)         True    You can have an abstract class without any abstract methods.


14. ( 6 points ) Assume someone wishes to represent the following scenario: A man, along with his pet dog and cat, are on an island with a date tree and a palm tree. The trees grow fruit. The pets can climb trees so they and the man can eat the food. All of these living beings have a name .                                                                        

Describe the structure of the program. Tell what classes, interfaces, and abstract classes you might use, and what methods and instance variables would be declared in each.

Interface: 
    LivingBeing has a constant NAME
Abstract Classes:
    Tree implements LivingBeing with method growFruit()
    Animal implements LivingBeing with method eat()
Classes : Island composed of DateTree, PalmTree, Man, Cat, Dog
    Man extends Animal  
    Pet extends Animal 
    DateTree extends Tree
    PalmTree extends Tree
    Pet extends Animal with an extra method climb()l
    Cat extends Pet  
    Dog extends Pet

Note: This is just one of the many possible answers.   

15. ( 8 points )Extra credit question : What is a marker/tag interface ? Give example.       

A marker/tag interface is an interface which doesn't define any methods or filelds i.e. such interfaces have no behavior. It is used to mark or tag a class which defines a certain capability.

Example: lang.Cloneable, util.EventListener, io.Serializable, etc.