Anda di halaman 1dari 13

University of Waterloo

CS115 Waterloo
Midterm Examination
Term: Spring Year: 2012

Disclaimer:
CS115 material may change from semester to semester, and
different instructors may stress different concepts. So, your
midterm may cover material not covered on this exam, or may
cover similar material in a different way.
If your instructor indicates that a Reference Sheet will be provided,
it will likely be similar to the one attached to this exam, but may
not be identical. Additional functions may be added, and others
removed.

General Information about using this exam:


Solutions for this exam will not be publicly available.
Students are encouraged to test their solutions themselves to see if
they are correct, and to visit CS115 office hours for assistance.
Students should not just "study to" this exam. Each midterm is
different.
You may use this exam in several helpful ways:
o As a practice exam: set yourself 2 hours to complete it
without using any additional aids. See if you are comfortable
with how you felt about the process, and get assistance on the
topics you were uncertain about;
o As a source of additional problems to try;
o As inspiration for developing additional problems yourself
(try solving the abstract list functions using accumulative
recursion, the accumulative one using abstract list functions,
drawing memory diagrams, etc.);
o To practice writing code by hand, on some problems you
have not seen before.

CS115 Midterm Spring 2012 Page 1


University of Waterloo
CS115 Midterm Examination
Term: Spring Year: 2012

Date: Monday, June 18, 2012


Time: 7:00 8:50 pm
Instructor: Lori Case Last Name: ____________________________
Lecture Sections: 001
Exam Type: Closed book First Name: ____________________________
Additional Materials Allowed: None
ID: __ __ __ __ __ __ __ __

Signature: ____________________

Instructions: (Read carefully before the exam begins):


1. Before you begin, make certain that you have one Exam Marks Out Markers
Booklet with 11 pages, and an attached Reference Sheet. Question
Given Of Initials
2. The Scheme language level is Beginning Student
Scheme. 1 11
3. Supply exactly the parts of the design recipe requested
in each question. Unless otherwise told, to complete a 2 4
function means to provide just the function body.
3 4
4. You may use a helper function where you feel it is
needed. For each helper function, you are only required 4 4
to write the function header and body.
5. You may use any function defined in the exam as a 5 7
helper function for any other function.
6. All solutions must be placed in this booklet. 6 3
7. If you need more space to complete an answer, you are 7 5
likely writing too much. However, if you need more
space, use the last page, and indicate that you have done 8 6
so in the original question.
8. Relax! Read this instruction as often as needed. 9 6

Total 50

CS115 Midterm Spring 2012 Page 2


1. [11 marks] Complete the entries in the second column below by indicating the simplified final
value of the Scheme expression if the expression does not contain any errors. If there is an
error, write "ERROR " and briefly explain the error. Two examples are done for you.

Scheme code Write simplified final value or "ERROR " with


a brief explanation of the error
(* (sqrt 3) (remainder 5 3) ERROR: missing closing bracket
(sqr 6) 36
(+ (* 2 10) (* 10 -2) (* 5 1))

(define x 10)
(+ (x 10) 22)

(sqrt 100 25 36)

(define w 10)
(define (g y) (+ w (- y 1)))
(g 1)
(and (< 50 100) (zero? (- 2 1))
(substring "a" 3 10))

(substring "abcdefgh" 4 6)

(cond
[(odd? 10) 'One]
[(odd? 20) 'Two]
[(odd? 30) 'Three])
(cond
[(even? 10) 'One]
[(even? 20) 'Two]
[(even? 30) 'Three])
(define L (cons 6 (cons 7 empty)))
(rest (first (rest L)))

(define M (cons 'a (cons 'b (cons


'c (cons 'd empty)))))
(first (rest (rest (rest M))))

(posn-x (make-posn 'x 'y))

CS115 Midterm Spring 2012 Page 3


2. [4 marks] Consider the following definitions.

(define (check x y) (< x y))

Use these definitions to simplify the following Scheme expression. Be sure to write a
complete trace of all steps in the simplification process. Use one line per step. Not all the
lines may be needed.

(or (check (+ 6 4) (- 5 2)) false)

CS115 Midterm Spring 2012 Page 4


3. [4 marks] Consider the following definitions.

(define x 10)

Use these definitions to simplify the following Scheme expression. Be sure to write a
complete trace of all steps in the simplification process. Use one line per step. Not all the
lines may be needed.

(* (cond
[(odd? x) (* 2 3)]
[else 0])
x)

CS115 Midterm Spring 2012 Page 5


4. [4 marks] Write a function basic-translation that consumes a nonempty string s, and
produces a new string like s, except that the first letter of s is moved to the end and the string
"ay" is appended to the end of the revised string. For example:
(basic-translation "dog") => "ogday"
(basic-translation "b") => "bay"
(basic-translation "apple") => "ppleaay" (the translation
doesn't always make sense! )

[1 mark] Write the contract for basic-translation.


;; basic-translation:

[3 marks] Complete the function body.


(define (basic-translation s)

CS115 Midterm Spring 2012 Page 6


5. [7 marks] Write a Scheme function f (along with any needed helper functions) that
consumes two numbers x and y and produces the value as follows:

 
+ ,        
  
,  = 
0,       
   +   ,           ,

2 ,   < 0
where

 =  100,   = 0 
4 ,   > 0.

[6 marks] Write the function body.


(define (f x y)

[1 mark] Write a test for (f 0 1).

CS115 Midterm Spring 2012 Page 7


6. [3 marks] Consider the following structure:

(define-struct dog (name colour age))


;; A dog is a structure (make-dog n c a), where
;; * n is a string, for the name of the dog
;; * c is a symbol, for the colour of the dog
;; (for example, 'black, 'white, 'brown, etc.)
;; * a is a nat, for the age of the dog in years

[1 mark] Compete the constant definition create a constant my-dog, and set its value to be a
brown dog named Max, who is 3 years old.

(define my-dog

[1 mark] Based on the information provided in the data definition above, write the contract
for the selector function dog-colour.

;; dog-colour:

[1 mark] Complete the body of the function same-name? that consumes two dog
structures, dog1 and dog2, and produces true if they have the same name, and false
otherwise. The function has been started for you.

(define (same-name? dog1 dog2)


(equal?

CS115 Midterm Spring 2012 Page 8


7. [5 marks] Write the Scheme combine-posns that consumes two posns, p1 corresponding
to (x1,y1) and p2 corresponding to (x2,y2), and a symbol, action, and produces a new posn,
as follows:
if action is 'add, then the produced posn corresponds to (x1+x2, y1+y2),
if te action is 'sub, then the produced posn corresponds to (x1-x2, y1-y2),
for any other value of action, produce the posn (0,0).

[1 mark] Write an example for combine-posns using posn values corresponding to (2,3)
and (-2,-1) and the symbol 'add.

[4 marks] Write the body of the function.


(define (combine-posns p1 p2 action)

CS115 Midterm Spring 2012 Page 9


8. [6 marks] Write a function count-long that consumes a list of strings, L, and a natural
number, n, and produces the total number of characters in strings of length at least n. For
example,
(count-long (cons "abcde" (cons "an" (cons "1234 56"
(cons "a" (cons "abcd" empty))))) 5) => 12
(count-long (cons "abc" (cons "ab" (cons "a" empty))) 10)
=> 0
(count-long (cons "abc" (cons "ab" (cons "a" empty))) 0)
=> 6

[1 mark] Write the contract for the function.


;; count-long:

[5 marks] Write the body of the function.


(define (count-long L n)

CS115 Midterm Spring 2012 Page 10


9. [6 marks] Study the Scheme function modify, and then answer the questions that follow.

(define (modify L)
(cond
[(empty? L) (cons false empty)]
[(negative? (first L))
(cons (* 2.0 (first L)) (modify (rest L)))]
[(positive? (first L))
(cons (* 3 (first L)) (modify (rest L)))]
[else (cons 'zero empty)]))

[2 marks] Write a contract for modify (assume that there will be no errors when modify is
called).
;; modify:

[3 marks] What are the final values for the following expressions?
(modify empty)

(modify (cons -1 (cons 4 empty)))

(modify (cons 4 (cons -2 (cons 0 (cons 1 empty)))))

[1 mark] Is there any value of M which satisfies the following expression?


(modify M) => (cons false (cons 'zero (cons -18 empty)))
Either provide a value of M which satisfies this expression or briefly explain (in a sentence or
two) why no such value exists.

CS115 Midterm Spring 2012 Page 11


*** END OF EXAM ***

This page as been left blank intentionally. Use this space if necessary to complete your
answers. Make sure you note on the original page that more of your solution can be
found here, and be sure to label your work with the appropriate question number.

CS115 Midterm Spring 2012 Page 12


CS 115 Spring 2012 Midterm Exam
Reference Sheet

For this exam, the Scheme language level is Beginner Student Scheme. You may find
these Scheme functions helpful.

(cons v lst) constructs a list with first element v followed by list lst.
(empty? lst) produces true if lst is empty, and false otherwise.
(first lst) produces the first element in the nonempty list lst.
(length lst) produces the number of elements of the list lst.
(member v lst) produces true if v is in the list lst, and false otherwise.
(rest lst) produces the rest of the nonempty list lst.
(string-append s t) produces a string containing all the characters in s
followed by all the characters in t.
(string-length s) produces the number of characters in the string s.
(substring s a b) produces a string consisting of the characters in s from
positions a to b-1, inclusive.
(substring s a) produces a string consisting of the characters in s from
positions a to the end of the s.
(make-posn x y) creates a posn object.
(posn-x p) produces the x-value of posn p.
(posn-y p) produces the y-value of posn p.
(equal? x y) produces true if x and y are equal, false otherwise.
(expt x y) produces x raised to the power of y.
(even? n) produces true if n is even and false if n is odd. Note 0 is even.
(integer? n) produces true if n is an integer, false otherwise.
(max x y) produces the larger of x and y.
(min x y) produces the smaller of x and y.
(negative? n) produces true if a number n is less than 0, and false if it is
greater or equal to 0.
(odd? n) produces true if n is odd and false if n is even.
(positive? n) produces true if a number n is greater than 0, false if it is less
than or equal to 0.
(quotient m d) produces the quotient (integer part) when m is divided by d.
(remainder m d) produces the remainder when m is divided by d.

(sqrt x) produces x for a nonnegative value of x.


(sqr x) produces x*x.

(zero? x) produces true if x=0 and false otherwise.

(check-expect act exp) tests whether act and exp are equal.

CS115 Midterm Spring 2012 Page 13

Anda mungkin juga menyukai