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 Midterm Exam - Software Design & Implementation, Exams of Java Programming

The university of washington cse 331 midterm exam for the spring 2010 semester. The exam covers topics such as specification testing, representation invariants, and consistency. It includes multiple choice and true/false questions.

Typology: Exams

2012/2013

Uploaded on 04/08/2013

savitha_48
savitha_48 🇮🇳

3

(1)

120 documents

1 / 8

Toggle sidebar

Related documents


Partial preview of the text

Download University of Washington CSE 331 Midterm Exam - Software Design & Implementation and more Exams Java Programming in PDF only on Docsity! University of Washington CSE 331 Software Design & Implementation Spring 2010 Midterm exam Friday, April 23, 2010 Name: Solutions UW Net ID: This quiz is closed book, closed notes. You have 50 minutes to complete it. It contains 28 questions and 8 pages (including this one), totaling 100 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 26 3 16 4 6 5 8 6 20 7 24 Total 100 Initials: Solutions 1 TRUE/FALSE 1 True/False (2 points each) Circle the correct answer. T is true, F is false. 1. T / F When specification testing, it is good practice (but not required) to check that a RuntimeException is thrown when invalid input is passed to a method. 2. T / F The representation invariant (RI) is guaranteed to hold for a correct implementation of an immutable abstraction, when no method in the class is executing. 3. T / F The representation invariant (RI) is guaranteed to hold for a correct implementation of a mutable abstraction, when no method in the class is executing. 4. T / F It is a violation of the abstraction barrier for a class to directly use fields of its superclass. 5. T / F When the internal state of an object changes, its hash code must also change. 6. T / F A method may throw an exception if, and only if, the exception is listed in the throws clause of the method specification. 7. T / F A good black-box test should be designed to cover every branch of the code, because any untested code might harbor bugs. Black-box tests should not consider the implementation, only the specification. 8. T / F A class that represents a Cartesian point should include a method to compute the x and y coordinates, or the rho and theta coordinates, but not all 4 methods, because including all of these makes the implementation bulky, and clients can always do the transformation. These methods conceptually belong to the class, not to a client. Furthermore, the existence of these methods permits changing the implementation, and possibly enables more efficient operations. 9. T / F In a class that implements an immutable ADT, the representation should never change (after the constructor is exited). Benevolent side effects may change the concrete representation without affecting the abstract value. 10. T / F When the user passes an argument that violates the precondition, it is helpful to throw an exception and to document this behavior in the throws clause of the specification. It is helpful to throw the exception, but not to document it in the throws clause — that would make the specification inconsistent, as it would specify behavior when the precondition is not satisfied. Recall that at a call site x.f(y), an illegal value of y can violate the method precondition of f. Assume that there are no errors in the code that implements x’s class, and that x satisfies the rep invariant. 11. T / F At a call site x.f(y), it is possible for a value of x to violate the method precondition of f. Suppose that there are two different implementations C1 and C2 of a given ADT, and the implementations have different representation invariants. 12. T / F It is a violation of the abstraction barrier for client code to intermix objects of type C1 and C2 in computations, or to intermix them in collections such as List. 13. T / F It is a violation of the abstraction barrier for the code of class C1 to make calls against an object of class C2. 2 Initials: Solutions 3 COMPARING IMPLEMENTATIONS AND SPECIFICATIONS 19. (8 points) Consider the following four specifications for double log(double x), a method that returns the natural logarithm of the input x: A @requires x > 0 @return y such that |eˆy - x| <= 0.1 B @return y such that |eˆy - x| <= 0.001 @throws IllegalArgumentException if x <= 0 C @requires x > 0 @return y such that |eˆy - x| <= 0.001 D @return y such that |eˆy - x| <= 0.001 if x > 0 and Double.NEGATIVE_INFINITY if x <= 0 For each of the following pairs of specifications, circle the stronger specification, or circle “neither” if the two specifications are either equivalent or incomparable. (i) A B neither (ii) A C neither (iii) A D neither (iv) B C neither (v) B D neither (vi) C D neither 5 Initials: Solutions 4 SHORT ANSWER 4 Short answer 20. (3 points) Write two words that describe when (that is, under what circumstances) an implementation should check preconditions. (a) inexpensive (b) convenient (c) debugging “Before” and “after” a method body could be acceptable answers for a rep invariant, but “after” does not make sense for a precondition. 21. (4 points) How, if at all, are the RI and the AF related? Answer in 1 sentence. The domain of the AF is objects that satisfy the RI. 22. (4 points) Write the full transition relation for the following specification: requires x > 2 returns y such that y ≥ x int anyGte(int x) . . . . . . , 〈1,−1〉, 〈1,0〉, 〈1,1〉, . . . , 〈1,RuntimeException〉, . . . , 〈1, infinite loop〉, . . . . . . , 〈2,−1〉, 〈2,0〉, 〈2,1〉, . . . , 〈2,RuntimeException〉, . . . , 〈2, infinite loop〉, . . . 〈3,3〉, 〈3,4〉, 〈3,5〉, . . . 〈4,4〉, 〈4,5〉, 〈4,6〉, . . . . . . 23. (4 points) In no more than 2 sentences, what is the difference between verification and validation? Verification is the process of determining whether the implementation satisfies the specification. Validation is the process of determining whether the specification meets the functional (user) re- quirements of the system — whether it is fit for use. 24. (5 points) In 1 sentence each, state the two key limitations of Java constructors and why each are a limitation. (a) Constructors always return a new object, but for both space and time efficiency it can be desirable to re-use objects, as in the interning design pattern. (b) Constructors always return an object of the requested type, never a subtype, which is incon- venient when clients need a particular type. 6 Initials: Solutions 4 SHORT ANSWER 25. (8 points) A general way to generate tests is to divide the input domain into distinct partitions, then choose one input from each domain. (a) In one sentence, what property should be true of each partition? Each partition is uniform with respect to failure; that is, the program should either fail for every input in the partition, or succeed for every input in the partition. Partial credit was given for mentioning heuristics, such as code coverage — but recall that those heuristics are intended to approximate the real answer given here. The theory behind partition testing explains why those heuristics tend to work. Note that this question asked for a property of each partition, not a property of the set of partitions. Also, saying that the partitions should be distinct and exhaustive is merely restating the definition. (b) In one sentence, explain why violating that property can mean that the test suite fails to detect (some) errors. If a failing input exists, but every partition contains at least one succeeding input (which means that some partition contains both types of input), then the arbitrary choice could choose only succeeding tests and miss errors. (c) In one sentence, state how the property can be violated but the suite is still guaranteed to find all errors. If, for every distinct defect in the program, some partition contains only inputs that fail be- cause of that defect, then the test suite will catch all defects. A common incorrect answer is to test all inputs. This is guaranteed to find all errors, but it redefines the partitions to have one input per partition, in which case the uniformity condition on each partition is satisfied. It isn’t right to answer that you could over-partition, so that the partitions you use are sub- partitions of the coarsest (most efficient) partitions that satisfy the uniformity requirement. Another incorrect answer was boundary testing. This is a heuristic that aims to over-partition, but it provides no guarantees. 26. (6 points) In one sentence each, give the two most important reasons that throwing an exception is preferable to returning a special value. (a) A special value may be hard to distinguish from a real result. (b) Special values are error-prone: the programmer may forget to check them. (c) An exception can carry arbitrary data that may be useful in describing the problem. (d) It can simplify the program logic. Many people said an exception implements the “fail fast” paradigm. But special values can do this just as easily: the library returns a special value as soon as there is a problem, and the client checks for it. Likewise, exceptions do not prevent any more (or fewer) problems than special values, if each is used correctly. By contrast, exceptions give you “guaranteed fail”, which is different but also valuable. 27. (4 points) In the PS3 and PS4 testing file format, a single test file can contain multiple differently- named graphs. In one sentence, give a reason why the staff chose to have multiple distinct graphs in a single test file. (Hint: the answer is not “to reduce the number of files in the test suite.”) 7
Docsity logo



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