CIT 591 Midterm Exam - Fall, 2006
Name _________________________

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

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

2.  ( 3 points )

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

ii)                   When should float be used instead of double ?

iii)                 What is the problem with the test

if ( pi = = 3.14 ) { }

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;

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

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

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 ){} if( one = two ){} switch( flag ){} if( one == two ){} switch( initial ){} if( flag ){} 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 ;
}

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";`

```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?

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 !

## 7. (20 points) Give short precise answers

i)                    Which methods can change a String object ?

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

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 ?

iv)                 Interface methods are implicitly  ___________  and ___________  .

v)                  Interface fields are implicitly  ________   ________    and   ________  .

vi)                 What is an Adapter class ?

vii)               A class can implement  ________    (number) interfaces and it can extend  ________  (number) class(es).

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

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

## 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

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 -

b) Variable -

c) VARIABLE -

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

class Person{
public String name;

Person(name){
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

2. john.equals(kate)

3. kate.equals(john)

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

Car – SoundSystem   :                      ________     ____

Cow – Animal            :                    ________     ____

Jet – Plane                 :                    ________     ____

Plane – Vehicle         :                     ________     ____

Room – Wall             :                    ________     ____

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        ________     ____

ii) You can use         ________     ____        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);
}
}
```
` `
` `
`13. ( 8 points )State True or False`
`a)   __________  A test suite can contain calls to other test suites `
`b) __________  Declaring a class abstract prevents it from being instantiated.`
`c)__________  Interfaces can have static methods`
`d)__________  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.

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