Answer key for final exam in COM 1204, spring 2002. 37 students took the test. The low score was 37, the median was 67, and the high was 96. The arithmetic mean was 70.2, and the sample deviation was 16.9. For each alternative answer to a multiple choice question, the percentage of students who chose that answer is shown in square brackets to the right of the answer. (These percentages may not add up to 100% because not all questions were answered, and because of rounding.) For other questions, the percentage of credit that was earned by students is shown in square brackets following the question. Name_______________________________________ Final Exam. COM 1204. Object-oriented design. Spring 2002. Each true/false and multiple choice question is worth 2 points. Other questions are worth the number of points shown within brackets. F 1 All correct code is good code. [97%] F 2. All good code is correct code. [46%] F 3. All good code works. [59%] F 4. All code that works is correct. [57%] 5. In the Java expression x.m(y,z) , the method that is actually called is determined in part by ____ A. the type of x and the classes of y and z [11%] ____ B. the types of x, y, and z [ 3%] ____ C. forces beyond the ken of mortal men ____ D. the classes of x, y, and z ____ E. the class of x and the types of y and z [86%] 6. One non-technical barrier to reuse of software has been ____ A. virtual memory ____ B. garbage collection [16%] ____ C. the non-invented-here syndrome [54%] ____ D. public-key cryptography [ 3%] ____ E. UML class diagrams [27%] 7. One technical barrier to reuse of Java software has been ____ A. Scott McNealy's antipathy for Bill Gates ____ B. the lack of a grand unified field theory ____ C. Java's lack of support for parametric polymorphism [78%] ____ D. Java's support for dynamic method dispatch [19%] ____ E. the absence of enumeration types from Java [ 3%] 8. Which of the following methods is defined by java.lang.Object? ____ A. remove [27%] ____ B. hashValue [35%] ____ C. inundate [ 5%] ____ D. transcend [ 3%] ____ E. iterator [30%] 9. Java programs that handle mouse click events normally ____ A. bait the trap with cheddar cheese ____ B. squeek when the mouse is squeezed ____ C. create a MouseListener object and attach it to a window [95%] ____ D. busy-wait until the mouse button is pressed [ 5%] ____ E. pipe all MIDI data bytes through a BSD socket 10. Timothy Budd's code for the Queen class begins with class Queen { private int row; private int column; private Queen neighbor; This means that a Queen actually represents a ____ A. 3-dimensional matrix of squares [ 3%] ____ B. hereditary monarchy ____ C. linked list of queens [76%] ____ D. spreadsheet [ 3%] ____ E. pretentious data type [19%] 11. Which of the following classes implements the java.util.Map interface? ____ A. java.util.ArrayList ____ B. java.util.HashSet ____ C. java.util.TreeSet [11%] ____ D. java.util.HashMap [86%] ____ E. java.util.LinkedList [ 3%] 12. Which of the following Java expressions allocates heap memory? ____ A. x * y ____ B. x.length ____ C. new A() [78%] ____ D. x instanceof A [ 3%] ____ E. x[i] [19%] 13. In garbage-collected languages, it is usually better to represent a linked list using ____ A. a container class than to add link fields to the objects in the list [54%] ____ B. explicit deallocation than to rely on the garbage collector for storage management [ 3%] ____ C. link fields within each object in the list than to use a container class [24%] ____ D. hash tables that map each element of the list to the next than to use a traditional representation [14%] ____ E. double linking (both backwards and forwards) than to rely on single linking [ 5%] 14. A Java constructor ____ A. initializes the data fields of a new object [95%] ____ B. is just a method whose return type is void [ 5%] ____ C. builds the standard class libraries from source ____ D. translates assembly code into Java ____ E. automatically grinds the beans before brewing 15. In Java, which modifier declares that a field is immutable? ____ A. const [ 5%] ____ B. implements ____ C. synchronized ____ D. irascible ____ E. final [95%] 16. Which of the following programs did Sun Microsystems use to produce HTML documentation for Java's standard class libraries? ____ A. Microsoft PowerPoint ____ B. Eliza ____ C. Adobe Acrobat [ 3%] ____ D. javadoc [95%] ____ E. lcc [ 3%] 17. Which of the following patterns is most likely to be used in place of the Iterator pattern? ____ A. Pavilion ____ B. Singleton [ 3%] ____ C. Colonial ____ D. Visitor [97%] ____ E. Fractal 18. When compared to object-oriented design, data-oriented design typically places more emphasis on ____ A. relationships between classes [ 5%] ____ B. representation independence [ 8%] ____ C. behavior [14%] ____ D. representation details [70%] ____ E. encapsulation [ 3%] 19. One important characteristic of an abstract data type is that ____ A. its data fields are visible to its clients [ 3%] ____ B. its clients define its representation [27%] ____ C. it can always be allocated on a stack ____ D. its methods are abstract [24%] ____ E. its interface is distinct from its implementations [46%] 20. An expression is said to have a side effect if, in addition to computing a value, it ____ A. computes the value using some kind of iteration [ 5%] ____ B. takes more than a microsecond to compute the value ____ C. changes the state of the program or an i/o device [84%] ____ D. involves at least one method call [ 3%] ____ E. examines one or more fields of an object [ 8%] The next two questions involve the following Java program. public class A { public static void main (String[] args) { Object o1 = new A(); A o2 = new A(); B o3 = new B(); A o4 = o3; /* ... */ } void m (Object x) { System.out.print ("1 "); } void m (A x) { System.out.print ("2 "); } void m (B x) { System.out.print ("3 "); } private static final class B extends A { void m (Object x) { System.out.print ("4 "); } } } 21. What would this program print if /* ... */ were replaced by o2.m(o1); o2.m(o4); o3.m(o1); o4.m(o3); ____ A. 1 2 2 4 [ 3%] ____ B. 1 3 2 3 [14%] ____ C. 1 2 4 3 [73%] ____ D. 1 4 4 3 [ 3%] ____ E. 1 4 4 4 [ 8%] 22. What would this program print if /* ... */ were replaced by o4.m(o1); o3.m(o1); o2.m(o3); o4.m(o4); ____ A. 4 3 4 4 [ 3%] ____ B. 4 4 3 4 [38%] ____ C. 2 2 3 4 [ 5%] ____ D. 4 4 3 2 [46%] ____ E. 2 3 2 4 [ 8%] 23. During software design, it is usually a good idea to ____ A. identify all classes that will be used by the implementation [38%] ____ B. concentrate on the coding details and to forget about the clients' needs ____ C. postpone decisions that don't have to be made at the current level of design abstraction [62%] ____ D. publish the private data of each class so beta testers can suggest improvements ____ E. list the size of pixels of each GUI component [Note: Due to the instructor's error, this question had two correct answers.] 24. The components of an object-oriented design are often identified by using scenarios to identify the important ____ A. participles [ 5%] ____ B. adjectives [ 5%] ____ C. nouns [54%] ____ D. verbs [34%] ____ E. adverbs 25. [4 points] Write Java code for a method that, given a Set, returns the maximum element of the Set according to the elements' natural ordering. Please assume that all classes and interfaces in java.util.* have been imported. [33%] Object max (Set s) { return Collections.max (s); } // It was also possible to answer this question by writing // more complex code that iterates over the Set while using // the compareTo method of the java.util.Comparable interface. 26. [15 points] Suppose A, B, C, D, E, and F are Java classes whose subclass relationships are shown by the UML class diagram --- --- | A | | D | --- --- ^ ^ | | ----------- --- | | | E | --- --- --- | B | | C | ^ --- --- | --- | F | --- Suppose further that Java variables a, b, c, d, e, and f are defined by A a = new A(); B b = new B(); C c = new C(); D d = new D(); E e = new E(); F f = new F(); 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. a = (A) b; legal [89%] a = (A) c; legal [89%] a = (A) f; type error [86%] b = (B) a; ClassCastException [57%] b = (B) c; type error [70%] b = (B) e; type error [86%] c = (C) b; type error [73%] c = (C) e; type error [86%] c = (C) f; type error [86%] d = (D) c; type error [86%] d = (D) f; legal [84%] e = (E) d; ClassCastException [62%] e = (E) f; legal [86%] f = (F) c; type error [89%] f = (F) e; ClassCastException [65%] 27. [8 points] Write a Java class declaration for a StudentList class that will work properly with the following two methods, which are part of an instructor's grading program. Please assume that all classes and interfaces in java.util.* have been imported. // Given a StudentList, prints the name and current // average for every Student in the list. void printStudentList (StudentList students) { Iterator it = students.iterator(); while (it.hasNext()) { Student student = (Student) it.next(); String name = student.name(); int avg = student.average(); System.out.println (name + " " + avg); } } // Given a Student and a StudentList, adds the Student // to the StudentList. void addToStudentList (Student student, StudentList students) { students.add (student); } // This class represents an ordered list of students. [51%] class StudentList extends ArrayList { } // Although the one-line answer above is excellent, it was // also possible to answer this question by writing a lot // of code. The last two questions involve an immutable abstract data type named ChessPiece. All of this ADT's operations are public static methods of the ChessPiece class. The ChessPiece ADT and its operations are specified by Signature: rook: int x int -> ChessPiece knight: int x int -> ChessPiece bishop: int x int -> ChessPiece row: ChessPiece -> int column: ChessPiece -> int canAttack: ChessPiece x int x int -> boolean Algebraic specification: row (rook (r, c)) = r row (knight (r, c)) = r row (bishop (r, c)) = r column (rook (r, c)) = c column (knight (r, c)) = c column (bishop (r, c)) = c canAttack (rook (r, c), row, column) = (r = row) or (c = column) canAttack (knight (r, c), row, column) = ((|r - row| = 1) and (|c - column| = 2)) or ((|r - row| = 2) and (|c - column| = 1)) canAttack (bishop (r, c), row, column) = (|r - row| = |c - column|) where |x| is the absolute value of x. 28. [10 points] Write representation-independent client code for a method that, given a Set of ChessPiece objects and two int values r and c, returns true if and only at least one of the ChessPiece objects in the set can attack (in the sense defined by the canAttack operation) the square at row r and column c. import java.util.*; [62%] boolean someCanAttack (Set pieces, int r, int c) { if (pieces == null) return false; Iterator it = pieces.iterator(); while (it.hasNext()) { ChessPiece p = (ChessPiece) it.next(); if (ChessPiece.canAttack (p, r, c)) return true; } return false; } 29. [15 points] Write Java code for an implementation of ChessPiece. [77%] abstract class ChessPiece { public static ChessPiece rook (int r, int c) { return new Rook (r, c); } public static ChessPiece knight (int r, int c) { return new Knight (r, c); } public static ChessPiece bishop (int r, int c) { return new Bishop (r, c); } public static int row (ChessPiece p) { return p.r; } public static int column (ChessPiece p) { return p.c; } public static boolean canAttack (ChessPiece p, int r, int c) { return p.canAttackMethod (r, c); } int r; // can't be private because of Java lossage int c; // can't be private because of Java lossage private ChessPiece (int r, int c) { this.r = r; this.c = c; } abstract boolean canAttackMethod (int row, int column); private static class Rook extends ChessPiece { Rook (int r, int c) { super (r, c); } boolean canAttackMethod (int row, int column) { return (r == row) || (c == column); } } private static class Knight extends ChessPiece { Knight (int r, int c) { super (r, c); } boolean canAttackMethod (int row, int column) { // Note that a knight, unlike a rook or bishop, // does not attack its own square. int rdiff = Math.abs (r - row); int cdiff = Math.abs (c - column); return (rdiff == 1 && cdiff == 2) || (rdiff == 2 && cdiff == 1); } } private static class Bishop extends ChessPiece { Bishop (int r, int c) { super (r, c); } boolean canAttackMethod (int row, int column) { return Math.abs (r - row) == Math.abs (c - column); } } }