A random five-digit number: a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3

Similar documents
This exam is worth 70 points, or about 23% of your total course grade. The exam contains 15 questions.

Discussion section number. a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z

This exam is worth 30 points, or 18.75% of your total course grade. The exam contains

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work?


SCHEME AND CALCULATOR 5b

CS61A Midterm 2 Review (v1.1)

Write a procedure powerset which takes as its only argument a set S and returns the powerset of S.

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream?

cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name

This exam is worth 30 points, or about 28% of your total course grade. The exam contains

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

CS 61A Midterm #2 ver March 2, 1998 Exam version: A. Your name. login: cs61a- Discussion section number. TA's name

CS 275 Name Final Exam Solutions December 16, 2016

Structure and Interpretation of Computer Programs

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

CS 61A Midterm #2 - October 5, 1998

CSE413 Midterm. Question Max Points Total 100

Structure and Interpretation of Computer Programs

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

CS61A Notes Disc 11: Streams Streaming Along

CS3L Summer 2011 Final Exam, Part 2 You may leave when finished; or, you must stop promptly at 12:20

CS 415 Midterm Exam Spring SOLUTION

CS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Quiz 1. Queues , Fall 2007 Recitation 10 Solutions 1

Structure and Interpretation of Computer Programs

Discussion 4. Data Abstraction and Sequences

Structure and Interpretation of Computer Programs

4.2 Variations on a Scheme -- Lazy Evaluation

Discussion 11. Streams

CS450: Structure of Higher Level Languages Spring 2018 Assignment 7 Due: Wednesday, April 18, 2018

CS3L Summer 2011 Exam 2 Time: up to 170 minutes (you may leave when finished; or, you must stop promptly at noon)

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))


Turtles All The Way Down

Richard Feynman, Lectures on Computation

CS 61A Discussion 8: Scheme. March 23, 2017

CS 415 Midterm Exam Fall 2003

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

CS450 - Structure of Higher Level Languages

HIERARCHICAL DATA What is a Tree? How is it different from a Deep List? When would you use one over the other?

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

Lexical vs. Dynamic Scope

Intro. Scheme Basics. scm> 5 5. scm>

Exam 2. Name: UVa ID:

CSE341 Spring 2017, Final Examination June 8, 2017

TAIL RECURSION, SCOPE, AND PROJECT 4 11

Structure and Interpretation of Computer Programs

Evaluating Scheme Expressions

Administrivia. Simple data types

CS 415 Midterm Exam Spring 2002

Building a system for symbolic differentiation

Statistics Case Study 2000 M. J. Clancy and M. C. Linn

Fall 2017 December 4, 2017

Structure and Interpretation of Computer Programs

Princeton University Computer Science COS226: Data Structures and Algorithms. Midterm, Fall 2013

CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018

Introduction to Algorithms March 11, 2009 Massachusetts Institute of Technology Spring 2009 Professors Sivan Toledo and Alan Edelman Quiz 1

cs1120: Exam 1 Due: Wednesday, 06 October at 3:30pm (in class)

CSE341 Autumn 2017, Final Examination December 12, 2017

Sample midterm 1 #1. Problem 1 (What will Scheme print?).

Where The Objects Roam

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

Scheme Basics > (butfirst '(help!)) ()

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

Berkeley Scheme s OOP

cs1120: Exam 1 Due: Wednesday, 06 October at 3:30pm (in class)

Structure and Interpretation of Computer Programs

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

CSE341 Spring 2017, Final Examination June 8, 2017

Fall 2017 December 4, Scheme. Instructions

Department of Electrical Engineering and Computer Sciences Fall 2000 Instructor: Dan Garcia CS 3 Final Exam

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

1998 Midterm and Solutions

University of Massachusetts Lowell

6.824 Distributed System Engineering: Spring Quiz I Solutions

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Macros & Streams Spring 2018 Discussion 9: April 11, Macros

Documentation for LISP in BASIC

Normal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation?

CS 61AS Fall 2013 Final

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

Structure and Interpretation of Computer Programs Summer 2014 Midterm 1

Structure and Interpretation of Computer Programs

Solutions and grading standards

Sample Final Exam Questions

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise

6.033 Computer Systems Engineering: Spring Quiz I THIS IS AN OPEN BOOK, OPEN NOTES QUIZ. NO PHONES, NO COMPUTERS, NO LAPTOPS, NO PDAS, ETC.

MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science. Issued: Wed. 26 April 2017 Due: Wed.

Warm-up! CS 3 Final Review. Predict the output. Predict the Output. Predict the output. Predict the output. What is your favorite color?

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

Transcription:

CS 61A Final Exam Your name A random five-digit number: Circle the last two letters of your login (cs61a-xx) a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 a b c d e f g h i j k l m n o p q r s t u v w x y z This exam is worth 70 points, or about 23% of your total course grade. The exam contains 14 questions. This booklet contains 14 numbered pages including the cover page. Put all answers on these pages, please; don t hand in stray pieces of paper. This is an open book exam. When writing procedures, don t put in error checks. Assume that you will be given arguments of the correct type. If you want to use procedures defined in the book or reader as part of your solution to a programming problem, you must cite the page number on which it is defined so we know what you think it does. *** IMPORTANT *** Check here if you are one of the people with whom we arranged to replace a missed/missing exam with other exam scores: *** IMPORTANT *** If you have made grading complaints that have not yet been resolved, put the assignment name(s) here: 1 2 /7 3 /4 4 /7 5-6 /7 7 /6 8 /4 READ AND SIGN THIS: I certify that my answers to this exam are all my own work, and that I have not discussed the exam questions or answers with anyone prior to taking this exam. If I am taking this exam early, I certify that I shall not discuss the exam questions or answers with anyone until after the scheduled exam time. 9 /6 10 /4 11 /6 12 /6 13 /7 14 /6 total /70 1

Question 1 (3 points): What will the Scheme interpreter print in response to each of the following expressions? If any expression results in an error or infinite loop, just write Error. > (define (foo x) (define count (+ x count)) count) > (define count 0) > (foo 3) > (foo 4) > count Question 2 (4 points): (a) Give an example of something that is a pair but not a list. If there is no such thing, write impossible. (b) Give an example of something that is a list but not a pair. If there is no such thing, write impossible. (c) What will the Scheme interpreter print in response to the second expression below? If it results in an error or infinite loop, just write Error. > (define my-stream (cons-stream 1 my-stream)) > (list (pair? my-stream) (list? my-stream)) 2

Your five-digit number: Question 3 (4 points): What is the order of growth in time of each of the following procedures? Assume N is the length of the list. (define (pinky lst) (if (null? lst) 0 (+ (accumulate + 0 lst) (pinky (cdr lst))))) Θ(1) Θ(N) Θ(N 2 ) Θ(N 3 ) Θ(2 N ) (define (brain lst) (if (null? lst) 0 (+ (+ (car lst) (brain (cdr lst))) (brain (cdr lst)) ))) Θ(1) Θ(N) Θ(N 2 ) Θ(N 3 ) Θ(2 N ) 3

Question 4 (7 points): Consider the following code: STk> (define (foo x) (let ((x 7) (f (lambda (z) (+ z x)))) (set! x 3) (f 10) )) STk> (foo 9) Running it will create the following environment diagram. Complete the diagram by adding in five missing arrows and the result of the set!, then fill in the result of the final expression. p: (x) b: (let ((x 7)...)...) G foo E2 x 9 p: (x f) b: (set! x 3) (f 10) E1 z 10 E3 x 7 f p: (z) b: (+ z x) 4

Your five-digit number: Question 5 (3 points): Given the following code: (define (foo x y) (bar 1 y)) (define (bar a b) (* x y)) (foo 3 4) What is the result of the final expression using lexical scope? What is the result of the final expression using dynamic scope? (If any expression causes an error, just write Error.) Which does Scheme use? Lexical scope Dynamic scope Question 6 (4 points): Pick the best answer for the questions below. (a) Ben Bitdiddle is able to send chat messages to Alyssa P. Hacker, but he isn t getting the replies she sends. Which of the following is most likely the problem? His client didn t set a callback. His client didn t finish the three-way handshake. The server isn t sending out updated client-lists when clients log on. The server isn t forwarding chat messages to their destinations. (b) Which of the following fixes for the Therac-25 would have prevented the most accidents? Add hardware interlocks, like the earlier Therac-20. Log any software-detected abnormalities. Ask operators to confirm high radiation doses. Use serializers to prevent concurrency issues. 5

Question 7 (6 points): (a) Consider the following code: > (define cereal-1 (make-serializer)) > (define cereal-2 (make-serializer)) > (define z 10) > (parallel-execute (cereal-1 (cereal-2 (lambda () (set! z (+ z 20))))) (cereal-2 (cereal-1 (lambda () (set! z 42)))) ) Which of the following problems are possible with the code above? Y N Incorrect results Y N Deadlock Y N Inefficiency (missed opportunity for parallelism) (b) Consider the following code: > (define x 5) > (define y 6) > (define x-serializer (make-serializer)) > (define y-serializer (make-serializer)) > (parallel-execute (x-serializer (y-serializer (lambda () (set! x (+ x 5))))) (x-serializer (y-serializer (lambda () (set! x (* x x))))) (x-serializer (y-serializer (lambda () (set! y (+ y 2))))) ) Which of the following problems are possible with the code above? Y N Incorrect results Y N Deadlock Y N Inefficiency (missed opportunity for parallelism) 6

Your five-digit number: Question 8 (4 points): Consider the following input to the lazy evaluator: (define count 0) (define (akbar arg) (let ((hoho count)) (set! count 3) (set! hoho arg) hoho)) (define (jeff) (set! count 5) 42) (define binky (akbar (jeff))) After evaluating these expressions, what is the value of count? What is the value of binky? (If any expression generates an error or infinite loop, just write Error. either value is a promise, just write Promise.) If 7

Question 9 (6 points): Consider the following MapReduce query: (define (count-words input) (list (make-kv-pair (kv-key input) (length (kv-value input))) )) (mapreduce count-words + 0 "/shakespeare") The result is a stream of key-value pairs, where the keys are the names of Shakespeare plays, and the values are the number of words in the play. ((a-lovers-complaint. 2568) (a-midsummer-nights-dream. 17608)...) (a) Change either the mapper or the reducer (but not both) to find the length of the longest line in each play. (You can use an existing Scheme primitive, or write an entirely new procedure. If you change the reducer, you can also change the base case.) Show your new call to mapreduce. (b) Change either the mapper or the reducer (but not both) to count the number of times the word thou appears in each play. (You can use an existing Scheme primitive, or write an entirely new procedure. If you change the reducer, you can also change the base case.) Show your new call to mapreduce. 8

Your five-digit number: Question 10 (4 points): The procedure largest-value takes a list of key-value pairs and returns the pair with the largest value: > (define assoc-list (list (make-kv-pair a 1) (make-kv-pair b 42) (make-kv-pair c 9005) )) > (largest-value assoc-list) (c. 9005) You may assume the argument to largest-value is never the empty list. Complete this implementation of largest-value by defining lv-helper. (define (largest-value assoc-list) (accumulate lv-helper (car assoc-list) (cdr assoc-list) )) 9

Question 11 (6 points): The function check-children is supposed to return #t if every node in a Tree has a datum equal to its number of children. Fix all errors and data abstraction violations (DAVs) in the following incorrect implementation of check-children. Note: although this question is worth 5 points, there may be anywhere from 1 to 10 mistakes in this code. (define (check-children tree) (or (= (first tree) (length (cdr tree))) (accumulate (lambda (x y) (and x y)) #f (every check-children (cdr tree) )))) 10

Your five-digit number: Question 12 (6 points): Define the following stream. You may use helper procedures. > (ss strm 15) (1 1 2 1 2 3 1 2 3 4 1 2 3 4 5...) (Note: the underlines are just to clarify the problem for you; this is a single stream of numbers.) 11

Question 13 (7 points): Consider the following object-oriented code for an animal class: ;; The animal class has a last-eaten instance variable ;; that always contains the thing that was last eaten. (define-class (animal) (instance-vars (last-eaten #f)) (method (eat something) (set! last-eaten something))) (define bugs-bunny (instantiate animal)) (ask bugs-bunny eat carrot) (a) We want to make a fish class. (A fish is a kind of animal.) Find and fix the problems in the fish class by adding any necessary code. There are at most four problems to fix. (define-class (fish) (instance-vars (everything-eaten ()) (method (eat something) (cons something everything-eaten) ) (method (worms-eaten) (length (filter (lambda (thing-eaten) (equal? thing-eaten worm)) everything-eaten) ))) Question 13 continues on the next page. 12

Your five-digit number: Question 13 continued: (b) A goldfish behaves exactly like a fish, but also has an owner. We are going to write a goldfish class. Y N Should you make fish a parent of goldfish? Y N Should you make animal a parent of goldfish? Y N Should you include an instance variable (or instantiation variable) called owner in the goldfish class? Y N Should you include an instance variable (or instantiation variable) called last-eaten in the goldfish class? Y N Should you include an instance variable (or instantiation variable) called everything-eaten in the goldfish class? Y N Should you write an owner method for the goldfish class? Y N Should you write a new version of the eat method? Y N Should you write a new version of the worms-eaten method? 13

Question 14 (6 points): Write logic (query) language rules for every-other, a relation between two lists that is satisfied if and only if the second list is the same as the first list, but with every other element removed. > (every-other (frodo merry sam pippin)?x) (every-other (frodo merry sam pippin) (frodo sam)) > (every-other (gandalf)?x) (every-other (gandalf) (gandalf)) Do not use lisp-value! 14