Answer key for midterm in COM 1204, spring 2002. 40 students took the test. The low score was 43, the median was 77, and the high was 100. The arithmetic mean was 74.9, and the sample deviation was 11.9. For each question, the percentage of credit that was earned by students is shown in square brackets following the question. Name_______________________________________ Midterm. COM 1204. Object-oriented design. Spring 2002. Each multiple choice question is worth 4 points. Other questions are worth the number of points shown within brackets. 1. The abstraction barrier separates ____ A. vapid code from vituperative code _XX_ B. clients from implementors [82.5%] ____ C. abstract classes from concrete classes ____ D. interfaces from outerfaces ____ E. static methods from dynamic methods 2. A specification is a kind of ____ A. medieval torture device ____ B. subsidiary morphological conundrum _XX_ C. contract between clients and implementors [75.0%] ____ D. guess as to what the code will eventually look like ____ E. secret code that managers use for sensitive email 3. Representation independence and information hiding increase the ____ A. probability that a software project will fail ____ B. global interdependence of representations _XX_ C. interchangeability of software parts [90.0%] ____ D. visibility of class attributes ____ E. distributed knowledge of implementation details 4. Algebraic specifications are most appropriate for specifying ____ A. mutable container classes ____ B. the Iterator objects of a Java Collection ____ C. programs written in assembly language _XX_ D. immutable abstract data types [95.0%] ____ E. the virtual memory subsystem of an OS kernel 5. Telling you how an assignment must arrive without telling you which email software to use to send it is an example of ____ A. a C++ destructor _XX_ B. responsibility-driven design [100.0%] ____ C. proof-carrying code ____ D. civil disobedience ____ E. finalization-resisting objects 6. A CRC card describes _XX_ A. component, responsibility, collaborator [95.0%] ____ B. circumstance, revision, change history ____ C. covariance, rectitude, contravariance ____ D. collation sequence, recursion, coercion ____ E. consistency, rotation, circularity 7. Rebecca Wirfs-Brock's Responsibility-Driven Design emphasizes ____ A. indentation ____ B. control flow ____ C. overloading ____ D. UML notation _XX_ E. behavior [57.5%] 8. In Java, a sum type is most likely to be represented by ____ A. a static final variable _XX_ B. an abstract class [42.5%] ____ C. a dynamic method ____ D. a hash table ____ E. a for loop 9. In Java, a product type is most likely to be represented by ____ A. a float ____ B. a static method _XX_ C. a class [47.5%] ____ D. a thread ____ E. a while loop 10. Which of the following patterns is most common in Java code? ____ A. the Catharsis pattern _XX_ B. case dispatch on a sum type [62.5%] ____ C. the Interpreter pattern ____ D. dynamic recycling of conceptual paradigms ____ E. the Amansuensis pattern 11. The public, protected, and private keywords of Java control ____ A. undecidable modalities ____ B. arithmetic precision ____ C. just-in-time compilation ____ D. meta-reflection _XX_ E. visibility [97.5%] 12. Which of the following cannot be mentioned within a static method? ____ A. other static methods in the same class _XX_ B. the pseudo-variable this [90.0%] ____ C. the names of the arguments to the method ____ D. the name of the class that contains the method ____ E. the name of the method itself 13. Which of the following classes is _not_ defined in java.util? _XX_ A. MenuSelectionManager [87.5%] ____ B. Vector ____ C. HashMap ____ D. LinkedList ____ E. TreeSet 14. [20 points] Suppose A, B, C, D, and E are Java classes whose subclass relationships are shown by the UML class diagram --- --- | A | | E | --- --- ^ | ----------- | | --- --- | B | | D | --- --- ^ | --- | C | --- Suppose further that Java variables a, b, c, d, and e are defined by A a = new A(); B b = new B(); C c = new C(); D d = new D(); E e = new E(); For each of the following assignment statements, indicate whether the assignment would be legal if it were to appear immediately after the above declarations. If the assignment would not be legal, indicate whether it would result in a compile-time type error or a run-time ClassCastException. [65.8%] [Instructor's note on key: I had intended for each of these assignments to include a cast on the right hand side, as in a = (A) b; but I forgot to include these casts on the test. Thus all but the first two assignments give type errors. Because I may well have made the same mistake in lecture, and because I led students to expect that at least some of the assignments would give exceptions rather than type errors, I accepted "ClassCastException" as though it were a correct answer for the third (b = a;) and fifth (c = b;) assignments.] a = b; legal a = c; legal b = a; type error [but see note above] b = d; type error c = b; type error [but see note above] c = d; type error d = c; type error d = e; type error e = a; type error e = d; type error 15. [8 points] Write Java code for a static method named sort that takes one argument toSort, an array of int, and modifies toSort by sorting it in place. You may assume that all classes and interfaces in java.util.* have been imported. [45.3%] // This is the best of infinitely many correct answers. static void sort (int[] toSort) { Arrays.sort (toSort); } 16. [20 points] Shape is an immutable abstract data type. All of its operations are public static methods of the Shape class. The Shape ADT and its operations are specified by Signature: circle: double -> Shape square: double -> Shape rectangle: double x double -> Shape area: Shape -> double Algebraic specification: square (r) = rectangle (r, r) area (circle (r)) = Math.PI * r * r area (rectangle (r1, r2)) = r1 * r2 Note: Math.PI is a constant defined by the java.lang.Math class, which is automatically imported into Java programs. Write Java code for an implementation of Shape. [85.8%] // One possible answer. public class Shape { public static Shape circle (double r) { return new Shape (Math.PI * r * r); } public static Shape square (double r) { return rectangle (r, r); } public static Shape rectangle (double r1, double r2) { return new Shape (r1 * r2); } public static double area (Shape s) { return s.area; } private Shape (double area) { this.area = area; } private double area; }