Starting from:

$25

Computer Science 221 Compressed Form- Test


NOTE: The class was a 75 minute class, so this semester's test will probably be a little shorter. I strongly urge you to try answering the questions yourself before looking at my answers. 1. What is the difference between an interface and an implementation? Why is this an important distinction? 2. What is information hiding? Why is it important to good programming? 3. What is cohesion? Should cohesion be kept high or low? Why? 4. What is coupling? Should coupling be kept high or low? Why? 5. Consider these animals, listed in alphabetical order: blue whale, dolphin, fish, grouper, humpback whale, mammal, ocean dweller, oyster, salmon, scallop, shellfish, shrimp, snapper, whale Draw a UML diagram arranging these into an inheritance hierarchy. (Question 5 was a total disaster. I didn't know that there were college students who didn't know that whales are mammals.) 6. An inheritance hierarchy contains the classes: Animal, Mammal, Dog, Cat, Dalmatian, Bird, Chicken Mammal and Bird are subclasses of Animal. Dog and Cat are subclasses of Mammal. Dalmatian is a subclass of Dog. Chicken is a subclass of Bird. Which of these declarations and assignments are legal and which are illegal? Why? a. Animal a1 = new Dog(); b. Animal a2 = new Dalmatian(); c. Dalmatian d1 = new Dog(); d. Bird b1 = new Animal(); e. Dog d2 = new Dalmatian(); 7. Assume that these legal declarations have been made. Animal theAnimal = new Animal(); Mammal theMammal = new Mammal(); Dog theDog = new Dog(); Dalmatian theDalmatian = new Dalmatian(); Bird theBird = new Bird(); Which of these assignments are legal and which are illegal? Why? a. theBird = theAnimal; b. theAnimal = theDog; c. theDog = theMammal; d. theDog = theDalmatian; e. theDalmatian = theDog; 8. Write a class called Polygon. The only instance variable is numberOfSides, an int, which is private. You do not have to write a getter or setter for it. Write a constructor for Polygon that has the number of sides as its parameter. There are two methods. One is area(). This returns a double, and it always returns zero. The other is toString(), which returns a representation of the number of sides. 9. Write a class called Rectangle that is a subclass of Polygon. The instance variables are height and width, both double, which are private. You do not have to write getters or setters for them. Write a constructor for Rectangle that has the height and width as its parameters. There are two methods. One is area(). This returns a double, and it overrides the method in Polygon. The other is toString(), which returns a representation of the number of sides and the area. 10. For the classes Polygon and Rectangle from the previous questions, the following declarations have been made: Polygon p1 = new Polygon(4); Polygon p2 = new Rectangle(2, 3); Rectangle r = new Rectangle(4, 5); What does each of the following print? a. System.out.println(p1); b. System.out.println(p2); c. System.out.println(r); 11. From the animal hierarchy, the following declarations have been made: Animal a = new Dog(); Dog d1 = new Dog(); Mammal m = new Dog(); Dog d2 = new Dalmatian(); Dog d3 = d1; What are the static type and the dynamic type of each of those variables? Name Static type Dynamic type a d1 m d2 d3 12. Given the following, write code to call the draw() method in the Poodle class on the Poodle attached to variable d: Poodle poo = new Poodle("Fifi"); Dog d = poo;

More products