CS421 Fall 2005 Midterm Exam, Exams of Computer Science

The cs421 fall 2005 midterm exam for a computer science course. The exam covers various topics in ocaml programming, including function definitions, recursion, and type derivation. Students are required to solve multiple-choice and problem-solving questions within a given time limit and under strict exam conditions.

Typology: Exams

Pre 2010

Uploaded on 03/16/2009

koofers-user-rnq
koofers-user-rnq 🇺🇸

10 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS421 Fall 2005 Midterm
Thursday, October 13, 2005
You have 75 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 13 pages to the exam.
Please verify that you have all 13 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
9
10
6
9
10
15
14
17
10
10
9
12
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download CS421 Fall 2005 Midterm Exam and more Exams Computer Science in PDF only on Docsity!

CS421 Fall 2005 Midterm

Thursday, October 13, 2005

  • You have 75 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 13 pages to the exam. Please verify that you have all 13 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 9 10

CS 421 Midterm Name:____________________________________

  1. (6 pts total) Suppose that the following code is input into OCaml: let x = “Hello, ”;; let polite y = x^y;; let x = “Hi, ”;; let z = polite “Mr. Jones”;; let x = 3. For each of the following circle the correct choice. a. (2 pts) z will have a value of
  1. “Hi, Mr. Jones” 2) “Hello, Mr. Jones” b. (2 pts) The declaration let x = 3.2; will cause a type error.
  2. true 2) false c. (2 pts) x will have an end value of
  3. “Hi” 2) 3.
  1. (9 pts total) a. (3 pts) Write an OCaml function f:int -> int -> int that adds the square of its first argument to 2 times its second argument. Pay attention to the type given.

CS 421 Midterm Name:____________________________________

  1. (10 pts total) Given the following OCAML datatype: type int_seq = Null | Snoc of (int_seq * int) a, (5 pts) Write a recursive function fold : (int -> ‘a -> ‘a) -> ‘a -> int_seq -> ‘a that folds a function over an int_seq, with a given value for the base case. b. (5 pts) Using the function fold, and no other recursion, write a function prod: int_seq -> int that returns the product of all the elements in a list. The product of Null is 1. You should have prod (Snoc (Snoc (Snoc (Null, 2), 3) , 5)) = 30

CS 421 Midterm Name:____________________________________

  1. (15 pts total) Below I have given you an outline for the type derivation of the following expression: let rec f = fun x -> x in f 7 Please complete the derivation by giving the results that go in the lettered blanks. Put your answers next to the corresponding letters below the type derivation outline

(#5)__ |- (#6): (#7) (#8)__ |- (#9): (#10) (#11)__ |- (#12): (#13)


(#1)__ |- (fun x -> x):(#2)__ (#3)___ |- (f 7):(#4)____


_ { } |- (let rec f = fun x -> x in f 7) :int (#1) ________________________________________________________________ (#2) ________________________________________________________________ (#3) ________________________________________________________________ (#4) ________________________________________________________________ (#5) ________________________________________________________________ (#6) ________________________________________________________________ (#7) ________________________________________________________________ (#8) ________________________________________________________________ (#9) ________________________________________________________________ (#10) ________________________________________________________________ (#11) ________________________________________________________________ (#12) ________________________________________________________________ (#13) ________________________________________________________________

CS 421 Midterm Name:____________________________________

  1. (17 points) Given the following lambda expression: (λ x. λ y. x y x) (λ u. λ v. λ w. v u w) ((λ s. s) (λ t. t)) a. (5 pts) Evaluate this term as fully as possible using only Eager Evaluation: b. (5 pts) Evaluate this term as fully as possible using only Lazy Evaluation:

CS 421 Midterm Name:____________________________________ (7 cont) c. (6 pts) Reduce this term to αβ-normal form: (You may omit explicit mention or use of congruence closure)

CS 421 Midterm Name:______________________________

  1. (9 pts) ) Below are three finite state automata over the alphabet {f,g}, each followed by a list of five strings. For each finite state automata, circle each string in the list that follows it that is accepted by the regular expression, and put an X through each one that is not.
    1. gffgf 2) ffggg 3) fggfgggfg 4) fffggg 5) gggff
    2. gffgg 2) ffgff 3) fffffggg 4) fgfgfg 5) fgf
    3. gffggf 2)fffggg 3) fgfgfg 4) ggffgg 5) gfgfgff f f f g g g f

0 g g f f f f f f g g f f g g f

CS 421 Midterm Name:______________________________

  1. Extra Credit (12 pts total): The following is type may be used to represent terms of the lambda calculus as abstract syntax trees: type lamb_exp = Var of string | App of (lamb_exp * lamb_exp) | App(string*lamb_exp) a. (3pt) Write the function free_vars lamb_exp -> string list returning a list of all the names of variables having a free occurrence in a term. b. (6pts)Write a function subst_good : string -> string -> lamb_exp -> bool that tells when it is permissible to replace free occurrences of the variable with the name of the second string by the variable with the name of the first string in the given expression. (Idea: we want know if e[y/x]) is valid.) You may find it useful to write and use an auxiliary. You may always find it useful to use the previous function and List.mem : 'a -> 'a list -> bool for testing whether an element is in a list.

CS 421 Midterm Name:______________________________ Rules for type derivations: Constants:


Γ|- n : int (assuming n is an integer constant)


Γ|- true : bool Γ|- false : bool Variables:


Γ |- x : σ if Γ(x) = σ Primitive operators ( ⊕ ∈ { +, -, *, …}): Γ |- e 1 : int Γ |- e 2 : int Γ |- e 1 ⊕ e 2 : int Relations ( (^) ˜ ∈ { < , > , =, <=, >= }): Γ |- e 1 : int Γ |- e 2 : int Γ |- e 1 ˜ e 2 :bool Connectives : Γ |- e 1 : bool Γ |- e 2 : bool Γ |- e 1 : bool Γ |- e 2 : bool Γ |- e 1 && e 2 : bool Γ |- e 1 || e 2 : bool If_then_else rule: Γ |- e 1 : bool Γ |- e 2 : τ Γ |- e 3 : τ Γ |- (if e 1 then e 2 else e 3 ) : τ Application rule: fun rule: Γ |- e 1 : τ 1 → τ 2 Γ |- e 2 : τ 1 [x : τ 1 ] ∪ Γ |- e : τ 2 Γ |- (e 1 e 2 ) : τ 2 Γ |- fun x -> e : τ 1 → τ 2 let rule: let rec rule: Γ |- e 1 : τ 1 [x : τ 1 ] ∪ Γ |- e 2 : τ 2 [x: τ 1 ] ∪ Γ |- e 1 :τ 1 [x: τ 1 ] ∪ Γ |- e 2 :τ 2 (let x = e 1 in e 2 ) : τ 2 (let rec x = e 1 in e 2 ) : τ 2