Questions with Midterm Exam - Programming Languages and Compilers | CS 421, Exams of Computer Science

Material Type: Exam; Class: Progrmg Languages & Compilers; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Fall 2006;

Typology: Exams

Pre 2010

Uploaded on 03/16/2009

koofers-user-o95
koofers-user-o95 🇺🇸

10 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS421 Fall 2006 Midterm
Tuesday, October 10, 2006
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 14 pages to the exam.
Please verify that you have all 14 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
Total
10
10
9
10
15
20
14
12
10
100
12
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Questions with Midterm Exam - Programming Languages and Compilers | CS 421 and more Exams Computer Science in PDF only on Docsity!

CS421 Fall 2006 Midterm

Tuesday, October 10, 2006

  • 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 14 pages to the exam. Please verify that you have all 14 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 Total 10

CS 421 Midterm Name:____________________________________

  1. (10 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.2;; 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. d. (4pts) Write what the environment in effect at the end of the evaluation of the above declarations will be: Solution: {x → 3.2, z → “Hello, Mr. Jones”, polite → < y → x^y, {x → “Hello”}}
  1. (9 pts total) a. (3 pts) Write an OCaml function f:int -> int -> int that doubles the first argument and adds it to the second. Pay attention to the type given. Solution: let f x y = (2 * x) + y

CS 421 Midterm Name:____________________________________

  1. (10 pts total) What is printed for each of the following pieces of code? You may omit the final type information. a. (2pts) let a = print_string “a” in (fun x -> fun y -> print_string “b” ) (print_string “c” ) a;; Solution: abc b. (2pts) let a = (fun x -> fun y -> print_string “a”) in let b = (print_string “b”) in a b (print_string “c”);; Solution: bca c. (2pts) let a = print_string “a” in (fun x -> fun y -> print_string “b” ) a (print_string “c” );; Solution: acb d. (2pts) (fun x -> let a = print_string “a” in fun y -> print_string “b”) (print_string “c”);; Solution: ca e. (2pts) let c = (print_string “c”) in (fun x -> let a = print_string “a” in fun y -> print_string “b”) c;; Solution: ca

CS 421 Midterm Name:____________________________________

  1. (15 pts total) Given the following OCAML datatype: type ‘a btree = Node of (‘a * (‘a btree) * (‘a btree)) | Leaf of ‘a | Empty a. (5 pts) Write a recursive program btree_sum : int btree -> int that returns the sum of all the values in a given btree.You should have: btree_sum (Node(3, Leaf 5, Node(2, Empty, Leaf 4))) = 14 Solution: let rec btree_sum tree = (match tree with Empty -> 0 | Leaf n -> n | Node (n, left_tree, right_tree) -> n + (btree_sum left_tree) + (btree_sum right_tree));;

CS 421 Midterm Name:____________________________________

  1. (20 pts total) Below I have given you an outline for the type derivation of the following expression:

let rec f = fun x -> fun y-> x + y in f 3

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. You should answer using concrete types; there are no type variables required for this problem.


(#17) |- (#18): (#19)_ (#20) |- (#21): (#22)_


(#14)_ |- (#15): (#16)


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


(#1)_ |- (fun x -> fun y -> x+y):(#2)_ (#3)_ |- (f 3 ):(#4)_


_ { } |- (let rec f = fun x -> fun y-> x + y in f 3) :int -> int (#1) ____{f : int -> int -> int }__________________________________________ (#2) _____: int -> int -> int____________________________________________ (#3) ____{f : int -> int -> int }___________________________________________ (#4) _____: int -> int __________________________________________________ (#5) ____{x:int; f : int -> int -> int }_____________________________________ (#6) ____ fun y -> x+y_________________________________________________ (#7) _____: int -> int __________________________________________________ (#8) ____{f : int -> int -> int }____________________________________________ (#9) ______f__________________________________________________________ (#10) _____:int -> int -> int ____________________________________________ (#11) ____{f : int -> int -> int }__________________________________________

CS 421 Midterm Name:____________________________________ 5 (cont.) (20 pts total) Below I have given you an outline for the type derivation of the following expression:

let rec f = fun x -> fun y-> x + y in f 3

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. You should answer using concrete types; there are no type variables required for this problem.


(#17) |- (#18): (#19)_ (#20) |- (#21): (#22)_


(#14)_ |- (#15): (#16)


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


(#1)_ |- (fun x -> fun y -> x+y):(#2)_ (#3)_ |- (f 3 ):(#4)_


_ { } |- (let rec f = fun x -> fun y-> x + y in f 3) :int -> int (#12) ______ 3 _________________________________________________________ (#13) ______:int_______________________________________________________ (#14) ____{y:int, x:int; f : int -> int -> int }________________________________ (#15) ______ x+y ______________________________________________________ (#16) ______:int ______________________________________________________ (#17) ______ {y:int, x:int; f : int -> int -> int }______________________________ (#18) _______x________________________________________________________ (#19) _______:int _____________________________________________________ (#20) ______ {y:int, x:int; f : int -> int -> int }______________________________ (#21) _______y________________________________________________________ (#22) ______:int _____________________________________________________

CS 421 Midterm Name:____________________________________

  1. (12 points) Given the following lambda expression: (λ x. λ y. x y x) ((λ s. λ t. s t) (λ z. z)) In each of the evaluations below, show each step, and label it with the type of reduction or conversion performed. (You may omit explicit mention or use of congruence closure) a. (3 pts) Evaluate this term as fully as possible using only Eager Evaluation: Solution: (λ x. λ y. x y x) ((λ s. λ t. s t) (λ z. z))
  • β→ (λ x. λ y. x y x) (λ t. (λ z. z) t)
  • β→ λ y. (λ t. (λ z. z) t) y (λ t. (λ z. z) t) b. (3 pts) Evaluate this term as fully as possible using only Lazy Evaluation: Solution: (λ x. λ y. x y x) ((λ s. λ t. s t) (λ z. z))
  • β→ λ y. ((λ s. λ t. s t) (λ z. z)) y ((λ s. λ t. s t) (λ z. z))

CS 421 Midterm Name:____________________________________ 7 (cont) (12 points) Given the following lambda expression: (λ x. λ y. x y x) ((λ s. λ t. s t) (λ z. z)) In each of the evaluations below, show each step, and label it with the type of reduction or conversion performed. (You may omit explicit mention or use of congruence closure) c. (6 pts) Reduce this term to αβ-normal form: Solution: (λ x. λ y. x y x) ((λ s. λ t. s t) (λ z. z))

  • β→ (λ x. λ y. x y x) (λ t. (λ z. z) t)
  • β→ (λ x. λ y. x y x) (λ t. t)
  • β→ λ y. (λ t. t) y (λ t. t)
  • β→ λ y. y (λ t. t) ( You could have picked up with either the eager or the lazy evaluation, just as well)

CS 421 Midterm Name:______________________________

  1. Extra Credit (12 pts total): a. (5pts) Write the function List.map : ('a -> 'b) -> 'a list -> 'b list, without using explicit recursion, but using List.fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b instead.. You may not use any other predefined functions Solution: let map f l = List.fold_right (fun a -> fun bl -> (f a) :: bl) l [];; b. (10pts) Write the function List.map : ('a -> 'b) -> 'a list -> 'b list, without using explicit recursion, but using List.fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a instead. You may not use any other predefined functions. Solution: let map f l = List.fold_left (fun rec_val -> fun elt -> fun l -> rec_val ((f elt)::l)) (fun l -> l) l []

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