Structure and Interpretation of Computer Programs

Similar documents
Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Summer 2014 Midterm 2

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Summer 2014 Midterm 1

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Spring 2015 Midterm 2

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Fall 2016 Midterm 2

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Spring 2014 Final (with corrections)

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Final Exam Solutions. Structure and Interpretation of Computer Programs. Fall 2011 /12 /16 /12 /18 /10 /12 /80 INSTRUCTIONS

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

Midterm Exam 1 Solutions

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Spring 2019 Midterm 2

CS61A Midterm 2 Review (v1.1)

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

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

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

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Fall 2015 Midterm 1

SCHEME AND CALCULATOR 5b

Structure and Interpretation of Computer Programs Summer 2015 Midterm 2

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

Structure and Interpretation of Computer Programs Summer 2015 Midterm 1

Structure and Interpretation of Computer Programs

The exam is closed book, closed calculator, and closed notes except your one-page crib sheet.

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

Introduction to Fall 2008 Artificial Intelligence Midterm Exam

Structure and Interpretation of Computer Programs

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

This should prevent residual sexism, racism, favoritism lurking in the unconscious of your professor & TA from biasing grading!!

Fall 2017 December 4, Scheme. Instructions

Fall 2017 December 4, 2017

Structure and Interpretation of Computer Programs Fall 2015 Midterm 2

Structure and Interpretation of Computer Programs

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

FINAL REVIEW 13. December 4, Sample Final Questions

Structure and Interpretation of Computer Programs Spring 2017 Mock Midterm 1

Structure and Interpretation of Computer Programs Spring 2014 Test 2

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

Structure and Interpretation of Computer Programs

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


University of Massachusetts Lowell

Structure and Interpretation of Computer Programs

Sample Final Exam Questions

ˆ The exam is closed book, closed calculator, and closed notes except your one-page crib sheet.

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

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

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

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

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

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

Performance Assessment Spring 2001

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

MIDTERM EXAMINATION - CS130 - Spring 2005

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

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

Midterm Review. CS61A Summer Katya Stukalova Jerome Baek

The exam is closed book, closed calculator, and closed notes except your one-page crib sheet.

In addition to the correct answer, you MUST show all your work in order to receive full credit.

CSE341 Spring 2017, Final Examination June 8, 2017

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

YOUR NAME PLEASE: *** SOLUTIONS ***

Structure and Interpretation of Computer Programs

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

CS3: Introduction to Symbolic Programming. Lecture 14: Lists.

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

Structure and Interpretation of Computer Programs Spring 2016 Test 1

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

CSE341 Spring 2017, Final Examination June 8, 2017

April 2 to April 4, 2018

CSE 123: Computer Networks Fall Quarter, 2013 MIDTERM EXAM

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))

Final Exam. Introduction to Artificial Intelligence. CS 188 Spring 2010 INSTRUCTIONS. You have 3 hours.

Practice midterm exam

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

CSE341 Autumn 2017, Final Examination December 12, 2017

Part 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points)

Introduction to Artificial Intelligence Midterm 1. CS 188 Spring You have approximately 2 hours.

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))

Transcription:

CS 61A Fall 2014 Structure and Interpretation of Computer Programs Practice Final INSTRUCTIONS You have 3 hours to complete the exam. The exam is closed book, closed notes, closed computer, closed calculator, except one hand-written 8.5 11 crib sheet of your own creation and the three official 61A midterm study guides attached to the back of this exam (not included for the practice exam). Mark your answers ON THE EXAM ITSELF. If you are not sure of your answer you may wish to provide a brief explanation. Last name First name SID Login TA & section time Name of the person to your left Name of the person to your right All the work on this exam is my own. (please sign) For staff use only Q. 1 Q. 2 Q. 3 Q. 4 Q. 5 Total /20 /20 /14 /20 /6 /80

2 1. (20 points) Say What? (a) (6 pt) For each of the following call expressions, write would be output by the interactive Python interpreter. The first two rows have been provided as examples. In the column labeled Interactive Output, write all output that would be displayed during an interactive session, after entering each call expression. This output may have multiple lines. Whenever the interpreter would report an error, write Error. You should include any lines displayed before an error. Reminder: the interactive interpreter displays the value of a successfully evaluated expression, unless it is None. Assume that you have started Python 3 and executed the following statements: cookies = [ " cookies ", " monster "] monster = [" cookies ", cookies ] def more ( monster, cookies ): more = monster + cookies if monster [0][0] == cookies [0][0]: monster [1] = cookies [ 1:] return more Expression Interactive Output square(5) 25 1 / 0 Error [x % 3 for x in [1, 5, 6] if x % 2!= 0] [cookies[1], monster[2]] more(cookies, monster) monster cookies[1][0] cookies is monster[1]

Login: 3 (b) (8 pt) (Similar to Fall 2013 Final) Each of the following expressions evaluates to a Stream instance. For each one, write the values of the three elements in the stream. The first value of the first stream is filled in for you. Assume that you have started Python 3 and executed the following statements, in addition to the Stream class statement on your final study guide. def q(t): def compute_ rest (): return q(t. rest. rest ) return Stream (t. rest. first + t. first, compute_rest ) s = lambda t: Stream (t, lambda : s(t -1)) t = s (5) u = q(t) Stream Has the first three elements t 5,, u,, q(u),, (c) (6 pt) (Similar to Fall 2013 Final) For each of the following Scheme expressions, write the Scheme value to which it evaluates. The first three rows are completed for you. If evaluation causes an error, write Error. If evaluation never completes, write Forever. Hint: No dot should appear in a well-formed list. Assume that you have started the Project 4 Scheme interpreter and evaluated the following definitions. ( define b ( lambda () (b ))) Expression Evaluates to (* 5 5) 25 (1 2 3) (1 2 3) (/ 1 0) Error ((1. (2 3. (4))). 5) (cons 1 (list 2 3 (car (4 5)))) ((lambda () (b))) ((lambda (x) (x 2)) 4)

4 2. (20 points) Such Enviroment Diagram (a) (10 pt) (From CS61A Practice Problems - http://cs61a.org/problems) Draw in the environment diagram that results from executing the code below until the entire program is finished, an error occurs, or all frames are filled. A complete answer will: Add all names, labels, and parent annotations to all local frames. Add all values created during execution. Show the return value for each local frame. wow = 6 def much ( wow ): if much == wow : such = lambda wow : 5 def wow (): return such return wow such = lambda wow : 4 return wow () wow = much ( much ( much ))( wow )

Login: 5 (b) (10 pt) (Based off Lecture 15) Draw the environment diagram that results from executing the code below until the entire program is finished, an error occurs, or all frames are filled. Add all names, labels, and parent annotations to all local frames. Add all values created during execution. Show the return value for each local frame. def f(x): x = 4 w = [x, 5] def g(y): def h(z): nonlocal x x = x + y w [0] = x return x + y + z return h return g a = f (1) b = a (2) c = b (3) + b (4)

6 3. (14 points) Dressing Classy (a) (9 pt) For the following class definition, cross out any incorrect or unnecessary lines in the following code so that the doctests pass. Do not cross out class declarations or doctests. You can cross out any other line of code, including method declarations, and your final code should be as short as possible. class Wardrobe : """ >>> shirts = [ Shirt (" green "), Shirt (" blue ")] >>> winter = Wardrobe ( shirts ) >>> winter. count 2 >>> winter. wear () green >>> shirts [0]. stains 1 >>> winter. count 1 """ " def init (self, shirtslist ): self. shirts = shirtslist shirts = shirtslist @attribute @property def count ( self ): sum = 0 for i in shirts : for i in self. shirts : if i. washed : sum += 1 return sum def wear (): def wear ( self ): for s in self. shirts : for s in shirts : if s. washed : if self. washed : s. washed = False s. stains += 1 return s. color return s. color class Cloth : def init ( color ): def init (self, color ): self. color = color washed = True self. washed = True class Shirt ( Cloth ): def init ( color ): def init (self, color ): self. color = color self. stains = 0 Cloth. init (self, color )

Login: 7 (b) (5 pt) Implement an iterator for the wardrobe that only iterates over the shirts that have been washed. Fill in the blanks in the implementation of the iter method for the Wardrobe class. For this part, you may assume that the Wardrobe class stores the list of shirts in an instance attribute called shirts and that the wear method of the Wardrobe class has been correctly implemented. Each instance of a shirt has an boolean attribute washed that indicates whether it has been washed. Class Wardrobe : """ >>> shirts = [ Shirt (" yellow "), Shirt (" green "), Shirt (" blue ")] >>> winter = Wardrobe ( shirts ) >>> winter. wear () yellow >>> for option in winter :... print ( option )... green blue >>> len ( winter. shirts ) 3 """ " """ Assume the methods from Part A are implemented """ def iter ( self ): for : :

8 4. (20 points) Where s Groot? (a) (7 pt) Groot has hidden himself in a Tree object. Define a function wheres groot, which takes in a tree object (that may or may not contain the string Groot as a node), and returns Groot if it exists somewhere in the tree, and Nowhere if it does not. Here s the implementation of the Tree and Link classes. nil = " Troog " class Link : def init (self, first, rest = nil ): self. first = first self. rest = rest class Tree : def init (self, entry, branches = nil ): self. entry = entry self. branches = branches def wheres_ groot ( gtree ): """ >>> evil = Tree (" Ronan ", Link ( Tree (" Nebula "), Link ( Tree (" Korath ")))) >>> good = Tree (" Gamora ", Link ( Tree (" Rocket ", Link ( Tree (" Groot "))))) >>> grooted = tree (" Star - Lord ", Link (evil, Link ( good ))) >>> wheres_ groot ( evil ) Nowhere >>> wheres_ waldo ( grooted ) Groot """ if : children = : : return

Login: 9 (b) (8 pt) Implement deep-reverse, which takes in a Scheme list and reverses the entire list, all sublists, all sublists within that, etc. Hint: You can use the list? operator to determine whether something is a list. STk > (deep - reverse ( foo bar baz )) ( baz bar foo ) STk > (deep - reverse (1 (2 3) (4 (5 6) 7))) ((7 (6 5) 4) (3 2) 1) ( define (deep - reverse lst ) ( cond )) (c) (5 pt) Terminal SQL You ve decided to use the SQL Travel Agency to plan a vacation. Your travel agent hands you the following table called airport. create table airport as select " SFO " as orig, " NYC " as dest, 500 as price union select " SFO ", " MAD ", 3000 union select " NYC ", " LHR ", 600 union select " LHR ", " FRA ", 600 union select " LHR ", " MAD ", 400; Write a SQL query that finds combination of flights (that have at least two flights) that are under a set budget of $1750 ordered by the total trip cost. You may not need to use all of the provided lines. Here is the expected output: start end cost hops -------------------------- NYC MAD 1000 2 SFO LHR 1100 2 NYC FRA 1200 2 SFO MAD 1500 3 SFO FRA 1700 3 with as ( select union select ) select ;

10 5. (6 points) Interpretation (From Summer 2014 MT2) Select which function(s) you would have to modify in order to add the new syntax features in Calculator. For full credit, you must justify your answers with at most two sentences. (a) (1 pt) = (equality checker) e.g. (= 3 1) returns False Justification: calc eval calc apply Both Neither (b) (1 pt) or e.g. (or (= 5 2) (= 2 2) (\ 1 0)) returns True Justification: calc eval calc apply Both Neither (c) (1 pt) Creating and calling lambdas (Assume define has been implemented.) e.g. ( define square ( lambda (x) (* x x ))) ( square 4) Justification: calc eval calc apply Both Neither (d) (1 pt) Which of the following is not a benefit of Client-Server Architecture Creates a Abstraction Barrier Server Reuses Computation Both Neither Justification: (e) (2 pt) Consider the following function definition. def g(n): if n % 2 == 0 and g(n + 1) == 0: return 0 return 5 Circle the correct order of growth for a call to g(n): Θ(1) Θ(log n) Θ(n) Θ(n 2 ) Θ(b n )