Computer Science 61A - Spring 2006 - Harvey - Midterm 3 Solutions

Computer Science 61A - Spring 2006 - Harvey - Midterm 3 Solutions

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

View Full Document Right Arrow Icon
1 of 10 5/11/2006 12:10 AM CS 61A Spring 2006 Midterm 3 solutions 1. Box and pointer. > (let ((x (list 1 2 3))) (set-car! (cdr x) (cddr x)) x) (1 (3) 3) +-------------+ | | | V --------- --|------ --------- | | | | | | | | | /| ---->| | ------>| | ------>| | / | | | | | | : | | | | |/ | --|------ --:------ --|------ | : | V V V 1 2 3 (Some people had correct diagrams but wanted to print (1 3) or (1 3 3). The backbone of this list still has three pairs, so the list still has three elements! And the car of the second pair is a pair, not the number 3.) > (let ((x (list 1 2 3 4))) (set-car! (cddr x) (cons (cadr x) (cdddr x))) x) (1 2 (2 4) 4) ********* * | * 2 <-------- | -----------+ * | * | ********* | ^ | ! | ! V +---+---+ +---+---+ +-!-+---+ +---+---+ | | | | | | | ! | | | | /| x -------> | | | ----->| | | ----->| ! | ----->| | | / | | | | | | | | | | . | | | | |/ | +-|-+---+ +-|-+---+ +-.-+---+ +-|-+---+ | | . | | | . | V V V V 1 2 3 4 [In this problem a new pair is created. In the diagram above, the new pair is the one bordered by asterisks (*). That's because this problem invokes a constructor (CONS) rather than only mutators (SET-CxR!). The car of the new pair points to a number, whereas its cdr points to another pair. A very common error was to make an additional new pair with car 4 and cdr the empty list. But there's only one CONS in this problem, and it only makes one pair.] Scoring: One point for each diagram, one point for each print form. 2. Concurrency
Image of page 1

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

View Full Document Right Arrow Icon
2 of 10 5/11/2006 12:10 AM The crucial point that many people missed is that a serializer only protects against another process that uses the same serializer! Serializers don't magically know what variables you want to protect; all they know is that each serializer should only allow itself to be used by one process at a time. (a) BYE or BYEBYE (b) BYE, BYEBYE, HIHI, HIBYE, or BYEHI To get BYE, first process finishes, then second process runs. To get BYEBYE, second process finishes, then first process runs. To get HIHI, first process reads BAZ twice, then second process runs, then first process sets BAZ. To get HIBYE or BYEHI, the first process reads BAZ once, then the second process runs, then the first process reads BAZ again and sets BAZ. (There are two possible results because Scheme is not guaranteed to evaluate argument subexpressions left-to-right!) Scoring: Subtract 1/2 point for each missing answer, or each incorrect answer added; round down to an integer. 3. OOP short answer. The central point of this question is that in the OOP paradigm, objects are used for everything, and the structure of the object classes reflects the structure of the real-world situation being simulated. (a) parenthood The class VANILLA has the class SCOOP as its parent, because a vanilla scoop is a particular kind of scoop. Is a cone a special kind of scoop? No. Is a scoop a special kind of cone? No. So the correct answer is "Neither." Most people got this; the most common wrong answer was to say that SCOOP should have CONE as its parent, probably because a scoop can be viewed as *part of* a cone. But the parent/child relationship isn't "a part of"; it's "a kind of." (b) flavors method (method (flavors) (map (LAMBDA (S) (ASK S 'FLAVOR)) scoops)) You *could* write the CONE class so that its instance variable SCOOPS would be a list of flavors (i.e., names of flavors) instead of a list of scoop objects.
Image of page 2
Image of page 3
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