2008 spring final

2008 spring final - Final Examination Computer Science 312...

Info icon This preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Final Examination Computer Science 312, Cornell University 13 May 2008 1
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
1. True/False [10 pts] (parts a–e) a. A computation in a thunk is evaluated zero or one times. b. In the message-passing approach to concurrency, threads exchange information by storing messages into shared memory references. c. To avoid race conditions when writing a thread-safe ADT, mutators must in general acquire locks, but observers that have no side effects need not. d. Reference counting can fail to collect some unreachable objects. e. When the lambda calculus term MYAPP = λf.λx.xf is applied to two arguments f and x , i.e., ( MYAPP f x ) , the result is the same as the result of applying f to x . 2
Image of page 2
2. Specification and verification [19 pts] (parts a–d) The following is the interface and a partial implementation of a data abstraction. Unfortunately the implementer was a little confused about the rep invariant and abstraction function, and has gotten off to a bad start. (This code is also reproduced on page 12, which can be detached for easy reference.) signature BAG = sig (* A bag (or multiset) is a unordered collection of elements in which * a given element can appear any number of times. This sig is an * interface for a mutable bag of integers. * Examples: Ø, {0}, {-1, 10, 10} *) type bag (* create() is a new empty bag {} *) val create: unit -> bag (* empty(b) is whether b is empty *) val empty: bag -> bool (* contains(b, x) is whether x is in b *) val contains: bag * int -> bool (* Effects: add(b, x) adds one appearance of x to b. Examples: add({1}, 0) = {0, 1}, add({2}, 2) = {2,2} *) val add: bag * int -> unit (* remove(b,x) is b with one appearance of element x removed. * Examples: After remove(b, 0) with b = {0,0,1}, b = {0, 1}. * After remove(b, 0) with b = {1}, b = {1}. *) val remove: bag * int -> unit end structure Bag :> BAG = struct type bag = {elem: int, count: int} list ref fun create() = ref [] fun empty(b) = case !b of [] => true | _ => false fun contains(b, x) = List.exists (fn ({elem = x’, count = _}) => x = x’) (!b) fun add(b, x) = ... fun remove(b, x) = if contains(b, x) then b := map (fn ({elem = x’, count = n}) => if x = x’ then {elem = x’, count = n-1} else {elem = x’, count = n}) (!b) else () end 3
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
(a) [5 pts] Based on the implementations of the operations other than remove , give the rep invariant and abstraction function for this implementation. (b) [7 pts] The add operation is unimplemented. Write a correct implementation using Bag.contains and List.map . (c) [2 pts] Give a test case showing that remove does not work correctly. (d) [5 pts] Suppose you wanted to consider remove correct. How would you need to change the rep invariant? Give a new implementation of empty that works for the new rep invariant. ( contains would also need to be modified, but you don’t have to show that.) 4
Image of page 4
3. Asymptotic performance [32 pts] (parts a–e) Consider the following code for computing the median element of a list of elements. Recall that
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern