Computer Science 61A - Spring 2001 - Harvey - Midterm 2

# Computer Science 61A - Spring 2001 - Harvey - Midterm 2 -...

• Notes
• 7

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

CS61A, MT2, Spring 2001 CS61A Spring 2001 MT2 Professor Brian Harvey and Professor Dan Garcia Problem #1 Consider the following definitions: > (define c (cons '() '(lets (go) bears))) > (define a (append '() '(lets (go) bears))) > (define 1 (list '() '(lets (go) bears))) (a) What will Scheme print in response to the following expressions? If an expression produces an error message, you may just write "error"; you don't have to provide the exact text of the message. If the value of an expression is a procedure, just write "procedure"; you don't have to show the form in which Scheme prints procedures. Also, draw a box and pointer diagram for the value produced by each expression. > c > a > 1 (b) What will Scheme print in response to the following expressions? Do not draw the box and pointer diagrams for them. > (cdr c) > (cdr a) > (cdr 1) (1 of 7)1/27/2007 5:39:58 PM

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

CS61A, MT2, Spring 2001 Problem #2 As you already know, lists are made out of pairs. Suppose we want to use an OOP version of pairs to make lists in proper object-oriented style. Louis Reasoner gives the following implementation of OOP pairs: (define-class (pair the-car the-cdr) (method (length) (if (null? the-cdr) 1 (+ 1 (ask the-cdr 'length))))) (a) Happy with his implementation, Louis interacts the following way with his pairs (fill in the blanks): > (define (new-cons a b) (instantiate pair a b)) > (define (new-cdr p) (ask p 'the-cdr)) > (define my-list (new-cons 3 '())) > (ask my-list 'length) ________________________________________________ > (define other-list (new-cdr my-list)) > (ask other-list 'length) ________________________________________________ (b) Reimplement OOP-style lists so that the examples in part (a) return 1 and 0 as they should. You may create or modify classes, methods, and/or procedures as needed. You must use OOP style. Problem #3 (a) Complete the definition below for the lock class. When a lock is instantiated, it must be given a PIN. You can close a lock at any time, but you may only open it if you have the secret PIN (matching the one given when it was created). You cannot open or close a lock twice in a row. (An attempt to
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern