Structure and Interpretation of Computer Programs

Similar documents
Midterm Exam 1 Solutions

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 2012 Alternate Midterm 1

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

Structure and Interpretation of Computer Programs Fall 2015 Midterm 1

Structure and Interpretation of Computer Programs

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

Structure and Interpretation of Computer Programs Spring 2015 Midterm 2

Structure and Interpretation of Computer Programs

61A Lecture 3. Friday, September 5

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 2015 Midterm 1

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

CONTROL AND HIGHER ORDER FUNCTIONS 1

Structure and Interpretation of Computer Programs

CSE331 Winter 2014, Midterm Examination February 12, 2014

Structure and Interpretation of Computer Programs

61A Lecture 2. Wednesday, September 4, 2013

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

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

Introduction to Fall 2008 Artificial Intelligence Midterm Exam

ENVIRONMENT DIAGRAMS AND RECURSION 2

CSE331 Winter 2014, Midterm Examination February 12, 2014

Structure and Interpretation of Computer Programs Spring 2019 Midterm 2

Structure and Interpretation of Computer Programs Summer 2015 Midterm 2

61A LECTURE 7 DATA ABSTRACTION. Steven Tang and Eric Tzeng July 3, 2013

Structure and Interpretation of Computer Programs Spring 2017 Mock Midterm 1

61A Lecture 6. Friday, September 7

Structure and Interpretation of Computer Programs Spring 2014 Test 2

Structure and Interpretation of Computer Programs Fall 2015 Midterm 2

Structure and Interpretation of Computer Programs

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

CONTROL AND HIGHER ORDER FUNCTIONS 2

Structure and Interpretation of Computer Programs

CS150 Sample Final. Name: Section: A / B

Structure and Interpretation of Computer Programs

Control and Environments Fall 2017 Discussion 1: August 30, Control. If statements. Boolean Operators

Structure and Interpretation of Computer Programs Fall 2016 Midterm 2

Midterm Exam 2B Answer key

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

61A Lecture 2. Friday, August 28, 2015

cs61amt2_4 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

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

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

ITERATION AND RECURSION 3

CONTROL AND ENVIRONMENTS 1

Control and Environments Fall 2017 Discussion 1: August 30, 2017 Solutions. 1 Control. If statements. Boolean Operators

CS 1301 Exam 1 Spring 2011

CSE413 Midterm. Question Max Points Total 100

CSCE 110: Programming I

C ONTROL AND H IGHER O RDER F UNCTIONS

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

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

CS 1301 Exam 1 Fall 2010

Read and fill in this page now. Your instructional login (e.g., cs3-ab): Your lab section days and time: Name of the person sitting to your left:

CS150 Sample Final Solution

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Lecture #15: Generic Functions and Expressivity. Last modified: Wed Mar 1 15:51: CS61A: Lecture #16 1

All program statements you write should be syntactically correct. Partial credit is not guaranteed with incorrect use of syntax.

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

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 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

CS 61A Control and Environments Spring 2018 Discussion 1: January 24, Control. If statements. Boolean Operators

Piecewise Defined Functions

CS 1301 Exam 1 Spring 2014

An introduction to Scheme

Control Flow: Loop Statements

CS 1301 Exam 1 Fall 2010

CS 111X - Fall Test 1

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

CS 164 Handout 11. Midterm Examination. There are seven questions on the exam, each worth between 10 and 20 points.

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

Sample Final Exam Questions

Structure and Interpretation of Computer Programs

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

Read and fill in this page now. Your lab section day and time: Name of the person sitting to your left: Name of the person sitting to your right:

Lecture #3: Environments

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

CS 1301 CS1 with Robots Summer 2007 Exam 1

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

Structure and Interpretation of Computer Programs Spring 2016 Test 1

CSE 123: Computer Networks Fall Quarter, 2013 MIDTERM EXAM

Transcription:

CS 61A Structure and Interpretation of Computer Programs Fall 2011 Midterm Exam 1 INSTRUCTIONS You have 2 hours to complete the exam. The exam is closed book, closed notes, closed computer, closed calculator, except a one-page crib sheet of your own creation and the official 61A Midterm 1 Study Guide. Mark your answers ON THE EXAM ITSELF. If you are not sure of your answer you may wish to provide a brief explanation. All short answer sections can be successfully answered in a few sentences at most. 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 Total /10 /16 /14 /10 /50

2 THIS PAGE INTENTIONALLY LEFT BLANK

Login: 3 1. (10 points) Call Expressions Assume that you have started Python 3 and executed the following statements: from operator import add, mul def square(x): return mul(x, x) def curry(f): def g(x): return lambda y: f(x, y) return g For each of the following call expressions, write the value to which it evaluates in the current environment (which may differ from what is printed). If evaluating the call expression causes an error, write error and the reason for the error. (a) (2 pt) add(4, square(print(2))) (b) (2 pt) add(4, square(square(2))) (c) (2 pt) print(4, square(square(2))) (d) (2 pt) curry(add)(square(2))(4) (e) (2 pt) add(curry(square)(2), 4)

4 2. (16 points) Defining Functions (a) (5 pt) A function streak returns the greatest number of 1 s returned consecutively in n calls to coin. Its second argument coin is a random, non-pure function that returns 1 sometimes and 0 other times. For instance, streak would return 3, if 10 calls to coin returned these 10 values, in order: 0, 1, 1, 0, 1, 1, 1, 0, 0, 1 Your partner hands you the following implementation of streak moments before this question is due. You don t have time to rewrite it; you can only indent lines, dedent (un-indent) lines, and delete things. Mark changes to the code below so that it correctly implements streak, using the following symbols: Place a right arrow ( ) before a line to indent it by an additional 4 spaces. Place a left arrow ( ) before a line to dedent it by 4 spaces. Circle a part of the code to delete it. def streak(n, coin): """Return the greatest number of 1 s returned consecutively in n calls to coin. coin -- A function that takes zero arguments and returns 0 or 1. n -- A positive integer """ n, k = 0, 0, 1 total = 0 # consecutive 1 s since a 0 greatest = 0 # The greatest number of consecutive 1 s while k <= n and coin() == 1: if coin == 0 or coin(n) == 0 or coin() == 0: else: greatest, total = max(greatest, total), total, 0 total = total + 1 k = k + 1 n = n + 1 return greatest

Login: 5 (b) (5 pt) In Newton s method, the function iter improve may cycle between two different guesses. For instance, consider finding the zero of f(x) = x 3 2x + 2 (graphed below) using exact derivatives. A starting guess of 1 updates to 0, which updates to 1 again. The cyclic pattern 1, 0, 1 is called a length-two cycle, because it contains only two distinct elements before repeating. 4 3.2 2.4 1.6 0.8 2-1.5-1 -0.5 0 0.5 1 1.5 2-0.8 Write a new version of iter improve that returns False as soon as a length-two cycle of guesses occurs, but otherwise behaves the same as the implementation from class (and printed on your study guide). def iter_improve(update, done, guess=1, max_updates=1000):

6 (c) (4 pt) The figure below depicts an example piecewise function, f(x) = { x 2 x < 1 3 x x 1 3 2 1 2-1.5-1 -0.5 0 0.5 1 1.5 2-1 Define a Python function called piecewise that takes three arguments: two functions lower func and upper func, and a number cutoff. lower func and upper func each take a single number argument and return a single number value. piecewise returns a new function that takes a single number argument x and returns lower func(x) if x is less than cutoff, or upper func(x) if x is greater than or equal to cutoff. For example: >>> f = piecewise(lambda x: x*x, lambda x: 3-x, 1) >>> f(0.5) 0.25 >>> f(5) -2 def piecewise(lower_func, upper_func, cutoff): (d) (2 pt) Assume that you have started Python 3 and executed the following statement: def self_apply(f): return f(f) Write an expression in the blank operand position below, so that this call to self apply evaluates to 5: self_apply( )

Login: 7 3. (14 points) Environments (a) (6 pt) Fill in this environment diagram and expression tree that result from executing the Python code in the lower right of the page. A complete answer will: Turtles Complete all missing arrows. Add all local frames created by applying user-defined functions. Add all missing names and values. Add all missing statements, expressions, and return values in the expression tree. mutant(y): y, x = y+1, y+2 return ninja(y)/2 ninja(x): return x + 2 turtle(x) return mul(x, y) + 2 mutant(y) y, x = y+1, y+1 return ninja(x)/2 def mutant(y): y, x = y+1, y+2 return ninja(y)/2 def ninja(x): return x + 2 def turtle(x): return mul(x, y) + 2 ninja(x) y, ninja = 5, turtle mutant(y)

8 ck (b) (6 pt) Fill in this diagram of environments and values that result from executing the Python code in the box below. You do not need to draw an expression tree. A complete answer will: Complete all missing arrows. Add all local frames created by applying user-defined functions. Add all missing names and values. walker(x): def texas(ranger): return x + 3 return texas ranger(n): return x + walker(n)(n) x = 10 def walker(x): def texas(ranger): return x + 3 return texas def ranger(n): return x + walker(n)(n) texas = ranger ranger(2) (c) (2 pt) What is the value of the nested expression texas(ranger(2)), evaluated in the current environment that results from executing the code in the previous problem? If evaluating this expression causes an error, write error and the reason for the error.

Login: 9 4. (10 points) Data Abstraction An amount is a collection of nickels (5 cents) and pennies (1 cent), with the following behavior condition: If an amount a is constructed with n nickels and p pennies, then 5 nickels(a) + pennies(a) = 5 n + p. Consider the following implementation of an abstract data type for amounts. def make_amount(n, p): return (n, p) def nickels(a): return a[0] def pennies(a): return a[1] (a) (2 pt) Circle all abstraction barrier violations for this abstract data type in the following implementation of add amounts, if there are any. def add_amounts(a1, a2): n1 = nickels(a1) n2 = nickels(a2) n = n1 + n2 p = a1[1] + a2[1] return (n, p) (b) (4 pt) An amount is minimal if it has no more than 4 pennies. Redefine the constructor so that all amounts are minimal, but the behavior condition of the type still holds. def make_amount(n, p):

10 (c) (4 pt) Suppose we redefine make amount functionally, as follows: def make_amount(n, p): def dispatch(message): if message == 0: return n elif message == 1: return p return dispatch Redefine the selectors to implement a valid abstract data type for amounts using this constructor, in which all amounts are minimal. That is, pennies should never return a number greater than 4. def nickels(a): def pennies(a):