Regular Specification - Software Design and Implementation - Past Exam, Exams of Java Programming

Main points of this past exam are: Regular Specification, Ignore the Code, Compile Time, Declared Types, Run Time, Declared Types, Boolean, Abstract Value, Abstraction Function, Representation Invariant

Typology: Exams

2012/2013

Uploaded on 04/08/2013

savitha_48
savitha_48 🇮🇳

3

(1)

109 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
University of Washington
CSE 331 Software Design & Implementation
Winter 2011
Final exam
Wednesday, March 16, 2011
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 38 questions and
15 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 24
3 28
4 28
5 30
6 26
7 14
8 6
10 18
11 10
12 8
13 12
14 20
Total 224
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Regular Specification - Software Design and Implementation - Past Exam and more Exams Java Programming in PDF only on Docsity!

University of Washington

CSE 331 Software Design & Implementation

Winter 2011

Final exam

Wednesday, March 16, 2011

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 38 questions and 15 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 224

Initials: Solutions 2 MULTIPLE CHOICE

1 True/False

(2 points each) Circle the correct answer. T is true, F is false.

  1. T / F When reasoning about uses of an ADT, it is permissible to ignore the code.
  2. T / F When reasoning about uses of an ADT, it is permissible to ignore observers.
  3. T / F Given two MDDs (module dependency diagrams), the one with fewer edges is better. This is generally true, but is not guaranteed.
  4. T / F List is a true subtype of List.
  5. T / F Regular specification fields are used for values represented by a concrete field in the imple- mentation, and derived specification fields are used for values with no representation (as a concrete field) in the implementation.
  6. T / F For each concrete field in the implementation, there should be a specification field (of some variety) in the abstraction.

2 Multiple choice

Mark each of the following that can be true.

  1. (3 points) When is overloading resolved? (a) at compile time (b) at run time (c) none of the above a. at compile time
  2. (3 points) Overloading is resolved based on what information? (a) declared types (b) run-time types (object classes) (c) none of the above a. declared types
  3. (3 points) When is overriding resolved? (a) at compile time (b) at run time (c) none of the above b. at run time
  4. (3 points) Overriding is resolved based on what information? (a) declared types (b) run-time types (object classes) (c) none of the above b. run-time types, which are object classes

Initials: Solutions 3 SHORT ANSWER

  1. (12 points) Suppose that you have observed an error in an execution of your program. Give three ways to localize the defect to a small part of the program while debugging, and explain (in 1 phrase or sentence each). Give ways that are as different from one another as possible. The key idea is divide and conquer, and each answer is a technique for that.
    • assertions: if it fails, then the error must have occurred earlier in execution
    • unit testing: if it fails, then the error must occur in the small part of the program that it executes. This could be expressed in terms of doing the testing top-down or bottom-up, which are just approaches to unit testing.
    • regression testing: if it fails, then the error must be related to recently-changed code

Just “binary search” is too vague an answer. Any answer that is about minimizing the input is incorrect.

  1. (10 points) In one sentence each, give an advantage of debugging using a debugger, and an advantage of debugging using print statements. If there are multiple answers, choose the best or most important one. Assume the bug is not timing-related. (Hint: don’t give the answer, “you have to learn to use the debugger”; assume you are already proficient with it.) Advantage of using a debugger: Using a debugger is quicker: you don’t need to edit, recompile your program, and re-run the test case. Using a debugger lets you quickly get information about values you didn’t think of putting in the log, without going through the above process. Using a debugger does not clutter your code with debugging statements. Advantage of using print statements: Using a trace log lets you see multiple moments of time at once, and directly compare them in your text editor or otherwise. Using a trace log permits saving debugging information from the field or for later examination, especially by a different person. Print statements may impact timing less in multi-threaded programs (since most debuggers only pause the current thread). A less compelling reason is that using a trace log may permit more expressive formatting of data.
  2. (6 points) Lecture said that the set of all non-negative real numbers is not a valid choice for a decre- menting function. Would a closed set of real numbers work? (An example is all numbers x such that 0 ≤ x ≤ 1.) This set has a minimum, and you can compare any 2 real numbers. Answer, and give one sentence of explanation. No. It is possible to reduce the value indefinitely many times without ever reaching the minimum.

Initials: Solutions 3 SHORT ANSWER

  1. (6 points) Why did Fred Brooks call the man-month “mythical” in his eponymous book and essay? Answer in one sentence. Man-months are not fungible. Measuring a project’s cost in terms of them does not permit an accurate determination of the time to completion, given a certain number of workers.
  2. (6 points) In one sentence, what is the scientific method? Formulate a hypothesis, then perform a systematic, repeatable experiment that has the potential to refute it.
  3. (6 points) Which is more important to clients of an abstraction? The abstraction function, the repre- sentation invariant, or are they of equal importance? Explain in one sentence. Neither: both are irrelevant to the client, because both depend on the implementation.
  4. (6 points) Which gives more flexibility to a client (a user) of an ADT: an ADT that uses checked or unchecked exceptions? Explain why, in 1 sentence. Unchecked, because the client can choose to catch it or to ignore it if the client knows that it cannot happen.
  5. (6 points) Consider two types, String and @Date String, where the latter is a String that can be interpreted as a date, such as "July 4, 1776" or "3/16/2011". Draw the type hierarchy for these two types. The hierarchy may include a third type, if needed.

String 6

@Date String

Initials: Solutions 3 SHORT ANSWER

  1. (6 points) Would it be possible for an ADT to contain both producer and mutator methods? Why, or why not, would this make sense? Answer in no more than 2 sentences. It would be perfectly legal, but it is not encouraged by the CSE 331 methodology. Typically an object is put into a particular desired state either by being constructed in that state, or by being mutated into that state. If there are two ways to get the same result, then the interface may be excessively large and might be confusing to users.
  2. (8 points) Suppose that a program calls method M, which satisfies specification S. You have not examined nor verified the program’s source code, but it passes an exhaustive test suite (the suite tests every legal input and verifies all postconditions). Suppose that you replace the program’s use of M by a use of method M′, which satisfies specification S′, and S′^ is stronger than S. Does the program still pass its test suite? Explain why or why not in no more than two sentences. The program does not necessarily still pass its test suite. The program may accidentally depend on properties of the implementation of M, that are not satisfied by M′. If we had verified the program source code, using only S in our proof, then the program would still be guaranteed to pass its test suite when using M′.

Initials: Solutions 4 REASONING ABOUT CODE

4 Reasoning about code

  1. (6 points) Give the weakest precondition for the following code, with respect to the postcondition x > y. Assume that p is boolean and x and y are int.

p = x>y; if (p) { x++; } else { y = x + y; }

Answer: wp(“ if ... ”, x > y) = p ⇒ wp(“ x=x+1 ”, x > y) AND ¬p ⇒ wp(“ y=x+y ”, x > y) = p ⇒ x + 1 > y AND ¬p ⇒ x > x + y wp(“ p = x>y ”, p ⇒ x + 1 > y AND ¬p ⇒ x > x + y) = x > y ⇒ x + 1 > y AND ¬(x > y) ⇒ (x > x + y) = true AND ¬(x > y) ⇒ y < 0 = ¬(x > y) ⇒ y < 0 = x > y OR y < 0

Initials: Solutions 4 REASONING ABOUT CODE

  1. (6 points) Prove that the binarySearch routine (which appears on pages 9 and 15) terminates. Use max − min as the decrementing function. If it becomes zero or less, then the loop exits. Note that min ≤ mid ≤ max. Each of the branches reduces the decrementing function:
    • The first branch reduces it from non-zero to zero
    • The second branch reduces it by increasing min.
    • The third branch reduces it by decreasing max.

Therefore, the loop exits.

  1. (12 points) Prove that the binarySearch routine (which appears on pages 9 and 15) gives a correct answer, if it terminates. This property is called “partial correctness”. The loop invariant LI has two parts:
    • val ∈ a[min..max]; that is, val is an element of the subarray of a from indices min to max, inclusive. Equivalently, a[min] ≤ val ≤ a[max]
    • min ≤ max

Another way to state the LI is: ∃i.min ≤ i ≤ max ∧ a[i] = val A partial correctness proof has three parts:

PRE ⇒ LI • The first part of the LI is true because val ∈ a and a = a[min..max].

  • The second part of the LI is true because a is non-empty. LI ∧ pred { loop body } LI • The first part of the LI still holds, even though min and max have changed, because a[min] ≤ val and val ≤ a[max] and therefore val ∈ a[min..max]. (Note that a has not changed.)
  • The second part of the LI still holds, because min ≤ mid ≤ max. LI ∧ ¬pred ⇒ POST min = max ∧ val ∈ a[min..max] ⇒ val = a[max]

Initials: Solutions 4 REASONING ABOUT CODE

  1. (10 points) Consider the PriorityQueue.peek method:

class PriorityQueue ... // Retrieves, but does not remove, the head of this queue, // or returns null if this queue is empty. public @Nullable E peek() { ... } }

Suppose you want to use it in a fashion like this:

int myMethod(PriorityQueue myQueue) { return myQueue.peek().getMonth(); }

and you want a compile-time guarantee that no NullPointerException will be thrown — despite the fact that the documentation of peek states that an exception might be thrown, it will not in your circumstance. Describe how you could augment the type system to give this guarantee. Choose two relevant methods of PriorityQueue, and state how the modified type-checker would treat them. You need to introduce a parallel type qualifier hierarchy, exactly analogous to the @KeyFor hi- erarchy that is used to augment the Nullness Checker: while @Nullable and @NonNull are related to one another, they are not related to @KeyFor. The new annotation could be named @NonEmpty , indicating that the queue is non-empty. Type-checking of peek would know that if the argument is @NonEmpty , then the result is @NonNull. Type-checking of add would know that the result is @NonEmpty.

Initials: Solutions 5 CODE EXAMPLES

For the next 4 questions, assume you have this code:

class ListNode { Object data; ListNode next; void m(ListNode arg) { ... } }

  1. (2 points) Write a method body for m that performs assignment but not mutation to arg. (2 lines of code max; the code does not have to do anything sensible, but should not use bad style.) arg = null;
  2. (4 points) How could you prevent such an assignment from occurring, in standard Java? What would happen if a programmer wrote, compiled, and ran such code? (1 sentence each) Add final to the declaration, and the code will be flagged as a compile-time error.
  3. (2 points) Write a method body for m that performs mutation but not assignment to arg. (2 lines of code max; the code does not have to do anything sensible, but should not use bad style.) arg.data = null;
  4. (4 points) How could you prevent such mutation from occurring, in standard Java? What would happen if a programmer wrote, compiled, and ran such code? (1 sentence each) Pass in an immutable object that throws an exception when a mutating method is called. An excep- tion would occur at run time. Or, change the type to an immutable type so the method call is flagged as a compile-time error.

Initials: Solutions 6 DESIGN PATTERNS

6 Design patterns

  1. (10 points) Why can interning be applied only to immutable classes? (Answer in no more than 3 sentences.) The implementation of interning requires that the equals method uses abstract value equality; otherwise, a previously-constructed value would not be found. But, clients of interning require that the equals method uses behavioral, or eternal, equality, so that substitution of an interned value for a non-interned one is guaranteed to give the same behavior. These are compatible only for immutable classes.
  2. (10 points) The procedural pattern contains uses of instanceof. For example, we saw this in lecture:

// Example of procedural pattern: typechecking a programming language expression Type tcExpression(Expression e) { if (e instanceof PlusOp) { return tcPlusOp((PlusOp)e); } else if (e instanceof VarRef) { return tcVarRef((VarRef)e); } else ...

The visitor pattern achieves the same purpose, without any uses of instanceof. Why not? That is, what happened to them? They were replaced by dynamic dispatch, which selects the appropriate implementation of the accept method based on the type of the currently-visited element.