Docsity

CS421 Spring 2007 Midterm Exam, Exams of Computer Science

Toggle sidebar
bg1
CS421 Spring 2007 Midterm
Friday, February 9, 2007
•You have 50 minutes to complete this exam.
•This is a closed-book exam.. You are allowed one 3inch by 5 inch card of notes
prepared by yourself. This card is not to be shared. All other materials, besides
pens, pencils and erasers, are to be away.
•Do not share anything with other students. Do not talk to other students. Do not
look at another student’s exam. Do not expose your exam to easy viewing by
other students. Violation of any of these rules will count as cheating.
•If you believe there is an error, or an ambiguous question, you may seek
clarification from myself or one of the TAs. You must use a whisper, or write
your question out. Speaking out aloud is not allowed.
•Including this cover sheet and rules at the end, there are 8 pages to the exam.
Please verify that you have all 8 pages.
•Please write your name and NetID in the spaces above, and also at the top of every
page.
Name:
NetID:
1
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download CS421 Spring 2007 Midterm Exam and more Exams Computer Science in PDF only on Docsity!

CS421 Spring 2007 Midterm

Friday, February 9, 2007

  • You have 50 minutes to complete this exam.
  • This is a closedb ook exam.. You are allowed one 3inch by 5 inch card of notes prepared by yourself. This card is not to be shared. All other materials, besides pens, pencils and erasers, are to be away.
  • Do not share anything with other students. Do not talk to other students. Do not look at another student’s exam. Do not expose your exam to easy viewing by other students. Violation of any of these rules will count as cheating.
  • If you believe there is an error, or an ambiguous question, you may seek clarification from myself or one of the TAs. You must use a whisper, or write your question out. Speaking out aloud is not allowed.
  • Including this cover sheet and rules at the end, there are 8 pages to the exam. Please verify that you have all 8 pages.
  • Please write your name and NetID in the spaces above, and also at the top of every page.

Name:

NetID:

Problems Possible Points Points Earned 1 2 3 4 5 6 7 8 Pre Total Extra Credit 9 Post Total

CS 421 Midterm 1 Name:____________________________________

  1. (9 pts total) a. (3 pts) Write an OCaml function f:int > int > int that squares the difference of the two arguments and adds the result to the second. Pay attention to the type given. Solution: let f x y = let diff = x – y in (diff * diff) + y;; What is the result of each of the following applications: b. (2 pts) f 7 4;; Solution: 13 c. (2 pts) f (4,2);; Solution: A type error; f needs an int as its first argument, but it is being applied to a pair of ints. d. (2 pts) f 3;; Solution: a function of type :int > int that when applied to an argument y, squares (3 – y) and adds it to y.

CS 421 Midterm 1 Name:____________________________________

  1. (15 pts total) Describe the environment that results from the following set of declarations have been made in Ocaml: let a = 5;; let apair x = (x , a);; let a = 7;; let f y = apair (y + a);; let p = f 2;; You should assume the environment is empty before the declarations are made. Your answer should be a precise mathematical answer, with a precise description of values involved in the environment. Solution: {a → 7, apair > <x → (x, a), {a → 5}>, f → <y → apair (y + a), {a → 7, apair → <x → (x, a), {a → 5}>, }>, p → (9, 5)}

CS 421 Midterm 1 Name:____________________________________

  1. (10 pts) Write a tail recursive function loop_for : int > int > (int > ‘a > ‘a) > ‘a > ‘a with the following behavior: The first argument is to act as a counter, and is to be incremented by 1 on each successive call. The second argument is the upper bound for the iteration. If n is strictly less than m, then loop_for m n f x should return x. Otherwise, loop_for m n f x should iteratively apply f to the index m and the accumulated value x, and then repeat with an incremented counter and new accumulated value. (loop_for is a functional version of a forl oop.) The expression loop_for 1 n (fun i > fun a > i * a) 1 should calculate the factorial of n if n ≥ 0. Solution: let rec loop_for m n f x = if n < m then x else loop_for (m+1) n f (f m x)
  2. (10 pts) Write a function cut_off : int > int list > int list such that cut_off n lst returns the list of all elements in lst that are less than or equal to n. The elements should appear in the same order in the returned list as they did in the input list. Solution: let rec cut_off n lst = match lst with [ ] => [ ] | x::xs > if x <= n then x::cut_off n xs else cut_off n xs

CS 421 Midterm 1 Name:____________________________________

  1. (10 pts) Suppose lst is a list of pairs of integers (i.e. (int * int) list). Give values for mult_sum_base and mult_sum_step, so that List.fold_right mult_sum_step lst mult_sum_base would return the sum of the product of the pairs in the list. That is, for lst = [(a 1 ,b 1 );(a 2 ,b 2 );...; (an,bn)] it computes ((a 1 * b 1 ) + (a 2 * b 2 ) + ... + (an * bn)) If the list is empty, it should return 0. The type of List.fold_right is List.fold_right : ('a > 'b > 'b) > 'a list > 'b > 'b a. (1 pt) What is the type of mult_sum_base: Solution: int b. (2 pt) What is mult_sum_base: Solution: 0 c. (3 pts) What is the type of mult_sum_step: Solution: (int * int) > int > int d. (4 pts) What is mult_sum_step: Solution: fun (x, y) > fun r > (x * y) + r