Download University of Washington CSE 331 Software Design & Implementation Final Exam, Spring 2010 and more Exams Java Programming in PDF only on Docsity!
University of Washington
CSE 331 Software Design & Implementation
Spring 2010
Final exam
Monday, June 7, 2010
Name: Solutions
CSE Net ID (username):
UW Net ID (username):
This exam is closed book, closed notes. You have 110 minutes to complete it. It contains 25 questions and 13 pages (including this one), totaling 220 points. Before you start, please check your copy to make sure it is complete. Turn in all pages, together, when you are finished. Write your initials on the top of ALL pages. Please write neatly; we cannot give credit for what we cannot read. Good luck!
Page Max Score
Total 220
Initials: Solutions 1 TRUE/FALSE
1 True/False
(2 points each) Circle the correct answer. T is true, F is false.
- T / F In order to instantiate a class, it must have at least one public constructor.
In the following questions, consider two method specifications S1 and S2, where S1 is stronger than S2.
- T / F S1 may have a weaker (easier-to-satisfy) requires clause than S2.
- T / F S1 may have a stronger (harder-to-satisfy) requires clause than S2.
- T / F S1 may have fewer items in its modifies clause than S2.
- T / F S1 may have more items in its modifies clause than S2.
- T / F S1 may have a weaker (easier-to-satisfy) effects clause than S2.
- T / F S1 may have a stronger (harder-to-satisfy) effects clause than S2.
Initials: Solutions 2 EQUALITY
- (14 points) Consider the following subclass.
public class TwoDPoint extends OneDPoint { double y; public boolean equals(Object o) { if (o instanceof TwoDPoint) { TwoDPoint other = (TwoDPoint) o; return x == other.x && y == other.y; } else { return false; } } }
Indicate whether each of the following properties holds for the equals method, and if the answer is false (the property does not hold), give a counterexample.
(a) T / F Reflexivity Counterexample: N/A (b) T / F Symmetry Counterexample: OneDPoint a; a.x = 5; TwoDPoint b; b.x = 5; b.y = 10; a.equals(b) ⇒ true b.equals(a) ⇒ false (c) T / F Transitivity Counterexample: N/A
Initials: Solutions 2 EQUALITY
- (14 points) Consider the following different subclass.
public class TwoDPoint extends OneDPoint { protected double y; public boolean equals(Object o) { if (o instanceof OneDPoint) { return o.equals(this); } else if (o instanceof TwoDPoint) { TwoDPoint other = (TwoDPoint) o; return x == other.x && y == other.y; } else { return false; } } }
Indicate whether each of the following properties holds for the equals method, and if the answer is false (the property does not hold), give a counterexample.
(a) T / F Reflexivity Counterexample: For any TwoDPoint p , execution of p.equals(p) suffers an infinite loop and stack overflow. (b) T / F Symmetry Stack overflow does not violate symmetry, because the behavior of a.equals(b) is the same as b.equals(a). Counterexample: N/A (c) T / F Transitivity Counterexample: N/A
Initials: Solutions 3 SHORT ANSWER
3 Short answer
- (3 points) How many arguments does method Object.hashCode take? 1 (the receiver)
- (8 points) For each pattern, mark whether the the functionality and the interface is the same or differ- ent, compared to the delegate. Write the word “same” or “different” in each box. Pattern Functionality Interface Adapter same different Decorator different same Proxy same same
- (6 points) For each of the following situations, indicate whether throwing a checked or unchecked exception is more appropriate. Write “checked” or “unchecked” in the space provided.
(a) The client supplied an argument that violates a precondition. unchecked (b) The client specified a file name, but the file cannot be found. checked (c) The implementation contains a bug that its own self-checks detected. unchecked
- (4 points) What should you do first when a bug is reported to you (or you find it yourself)? Circle the best answer.
(a) explain the bug to a peer (b) fix the bug by changing the code (c) reproduce the bug (d) understand the bug
Reproduce the bug.
- (5 points) A tainted value is one that comes from arbitrary, unvalidated data, such as user input. An untainted value is one that can be trusted, such as a value that has been validated or was hard-coded in the program. It is safe to use untainted values where tainted values are needed, but not vice versa. Draw the type hierarchy for the qualifiers @Tainted and @Untainted. The hierarchy may introduce a third annotation, if needed.
@Tainted 6
@Untainted
Initials: Solutions 3 SHORT ANSWER
- (28 points) Suppose that you have defined the following type qualifiers:
- @Positive for values > 0
- @NonNegative for values ≥ 0
- @Negative for values < 0
These only apply to java.lang.Number and its subtypes (Integer, Double, etc.). Express the subtyping relationship among all the following types.
- Number
- Integer
- @Positive Number
- @NonNegative Number
- @Negative Number
- @Positive Integer
- @NonNegative Integer
- @Negative Integer
- @NonNegative Double
- List
- List<@NonNegative Number>
- List<? extends @NonNegative Number>
- List
- List<@Positive Integer> Please draw a subtyping diagram (please draw neatly!), with the following properties:
- Every subtype appears lower (on the page) than all its supertypes, and there is an arrow from the subtype up to the supertype.
- Draw as few arrows as possible: if A is a subtype of B, B is a subtype of C, and A is a subtype of C, don’t draw the A→C arrow that is implied by the other two. (If B isn’t in the diagram, then do draw the A→C arrow.)
- For brevity, you may use the following abbreviations: N for Number, I for Integer, D for Double, @P for @Positive, @NN for @NonNegative, @N for @Negative.
- Number and Integer are done for you (without the abbreviations).
- Tip: double-check that there are 14 boxes in your diagram when you are done.
Number
Integer
@Positive Integer
@NonNegative Integer @Negative Integer
@NonNegative Double @Positive Number
@NonNegative Number @Negative Number
List
List<@NonNegative Number>
List<? Extends @NonNegative Number>
List
List<@Positive Integer>
Initials: Solutions 3 SHORT ANSWER
- (12 points) Suppose that a programmer started with a program whose module dependence diagram (MDD) is M1, and performed a refactoring to end up with a program with improved design whose MDD is M2.
(a) Is it possible that M2 has fewer edges than M1? Explain why or why not. (1 sentence) Yes. The refactoring may have eliminated undesired coupling. (b) Is it possible that M2 has more edges than M1? Explain why or why not. (1 sentence) Yes. The refactoring may have introduced a new interface and replaced a dependence on a concrete class by a dependence on the interface, as shown in lecture.
- (8 points) What is the difference between efficiency and learnability in the context of interface usabil- ity? (1–2 sentences) Learnability is how quickly new users get used to the interface. Efficiency is how quickly experienced users can use the interface to do work.
- (8 points) Lecture gave an example of a visitor that could be applied to a tree. If the visitor code was applied to a cyclic data structure, such as a graph, the code would suffer an infinite loop and a stack overflow. How could the code be changed so that it would not suffer this problem? (≤2 sentences) Maintain a list of all nodes visited so far. If one is visited again, return immediately from that invocation of the visitor.
Initials: Solutions 3 SHORT ANSWER
- (6 points) Suppose that class C cannot be instantiated. How could a client use the class? (≤1 sentence) There are two reasons that a class cannot be instantiated: its constructors always throw an excep- tion, or the class is abstract. In either case, a client can call static methods that C defines. If the class is abstract, its non-static methods can be used by a subclass. (For full credit for the latter answer, you would have had to mention that the class was abstract.) “Via a factory method” is incorrect, because the factory would need to instantiate the class, and the question says the class cannot be instantiated.
- (12 points) You wish to deliver a program that does not suffer from representation exposure that can corrupt your data structures (violate the rep invariant). In 1 phrase each, give 4 approaches. Choose 4 approaches that are as different from one another as possible.
(a) design your data structures to be immutable (b) call checkRep frequently, to detect problems during testing (c) use an immutability type-checker (d) perform an inductive proof that the rep never escapes (e) perform defensive copies where appropriate
- (12 points) Give 4 strategies for creating a method stub (1 phrase each). Give strategies that are as different from one another as possible. Recall that a stub is a small, partially-functional implementation that permits testing client code before the method being stubbed has been fully implemented.
(a) Print a message describing the call, if the calling program doesn’t need a result (b) Generate a canned set of responses (c) Provide a partial implementation with a lookup table (d) Return an arbitrary or random value (e) Use an inefficient implementation
“Throw an exception” is not a good answer, because the client code must continue running after calling the client code.
- (10 points) Give an example of code that cannot go wrong at run time, but which the Java type system rejects (no more than 3 lines of code, 1 is enough). Explain why the Java type system rejects it and why it is actually safe (1 sentence each). Integer i = new Integer(22).clone(); The compile-time type of new Integer(22).clone() is Object , and an assignment of an Object value to an Integer variable is illegal. The the run-time type of new Integer(22).clone() is Integer , and so the assignment is always legal.
Initials: Solutions 4 CODE FOR ONEDPOINT
4 Code for OneDPoint
Questions 8–10 use the following code for a 1–dimensional point. You may rip out this page if you find doing so useful. You do not need to turn in this page.
public class OneDPoint { double x; public boolean equals(Object o) { if (o instanceof OneDPoint) { OneDPoint other = (OneDPoint) o; return x == other.x; } else { return false; } } }