Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

University of Washington CSE 331 Software Design & Implementation Final Exam, Spring 2010, Exams of Java Programming

The final exam for the university of washington cse 331 software design & implementation course from spring 2010. The exam covers topics such as method specifications, equality, and short answer questions related to programming concepts.

Typology: Exams

2012/2013

Uploaded on 04/08/2013

savitha_48
savitha_48 🇮🇳

3

(1)

120 documents

1 / 13

Toggle sidebar

Related documents


Partial preview of the text

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 2 14 4 14 5 14 6 14 7 26 8 28 9 16 10 28 11 40 12 26 Total 220 Initials: Solutions 1 TRUE/FALSE 1 True/False (2 points each) Circle the correct answer. T is true, F is false. 1. 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. 2. T / F S1 may have a weaker (easier-to-satisfy) requires clause than S2. 3. T / F S1 may have a stronger (harder-to-satisfy) requires clause than S2. 4. T / F S1 may have fewer items in its modifies clause than S2. 5. T / F S1 may have more items in its modifies clause than S2. 6. T / F S1 may have a weaker (easier-to-satisfy) effects clause than S2. 7. T / F S1 may have a stronger (harder-to-satisfy) effects clause than S2. 2 Initials: Solutions 2 EQUALITY 9. (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 5 Initials: Solutions 2 EQUALITY 10. (14 points) Consider the following different subclass. public class TwoDPoint extends OneDPoint { protected double y; public boolean equals(Object o) { if (o instanceof TwoDPoint) { TwoDPoint other = (TwoDPoint) o; return x == other.x && y == other.y; } else if (o instanceof OneDPoint) { return o.equals(this); } 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: N/A (c) T / F Transitivity Counterexample: TwoDPoint a; a.x = 5; a.y = 10; OneDPoint b; b.x = 5; TwoDPoint c; c.x = 5; c.y = 20; a.equals(b) ⇒ true b.equals(c) ⇒ true a.equals(c) ⇒ false 6 Initials: Solutions 3 SHORT ANSWER 3 Short answer 11. (3 points) How many arguments does method Object.hashCode take? 1 (the receiver) 12. (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 13. (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 14. (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. 15. (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 7 Initials: Solutions 3 SHORT ANSWER 18. (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. 19. (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. 20. (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. 10 Initials: Solutions 3 SHORT ANSWER 21. (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. 22. (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 23. (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. 24. (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. 11 Initials: Solutions 3 SHORT ANSWER 25. (26 points) Prove the correctness of the following loop. You can (and should) do so in 4 bullet points. Do not show any work between the two statements of the loop body. You need to state every fact that is relevant, but you do not need to justify facts based on the laws of arithmetic. All variables are integers, and you can ignore overflow. precondition PRE: r = 1 ∧ i = 0 ∧ n > 0 while (i != n) { r = r * x; i = i + 1; } postcondition POST : r = xn ∧ i = n You need to (a) provide a loop invariant LI, (b) show it is true initally, (c) show it is maintained by the loop, and (d) show it implies the postcondition. Let B be the loop body and pred ≡ i 6= n be the predicate that controls the loop. (a) Loop invariant LI: r= xi ∧ i≤ n (b) PRE⇒ LI obvious by substitution (c) LI ∧ pred { B } LIpost (r= xi ∧ i≤ n) ∧ i 6= n { r= r * x; i= i + 1 } rpost = xipost ∧ ipost ≤ n (d) LI ∧ ¬pred ⇒ POST obvious by substitution A commom mistake was assuming the inductive step is after one loop iteration rather than after zero. Another common mistake was noting that execution is guaranteed to execute the loop at least once; this is true here but irrelevant in the general case. Another common mistake was an assumption that the decrementing function indicates the number of loop iterations; it is a bound rather than an exact count. 12
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved