Docsity

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

Toggle sidebar
bg1
CS421 Summer 2006 Midterm
CS 421 — Introduction to Programming Languages
Summer Term 2006
11:30am–12:45pm, Thursday, June 29, 2006
The total time for this exam is 75 minutes.
Print your name and netid below. Also write your name at the bottom of each subsequent page.
Name:
Netid:
•This is a closed-book exam. You are allowed one 3 inch by 5 inch card of notes prepared
by yourself. You may write on both sides of the card. This card is not to be shared. All
other materials, besides pens, pencils, and erasers, are to be put away.
•Do not share anything with other students. Do not talk to other students. Do not look at
another student’s exam. Also, be careful to 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 must document your assump-
tions about what the question means. We are not allowed to answer questions about the exam
in class, and obviously cannot answer these questions for students taking the exam elsewhere.
•Including this cover sheet, rules at the end, and scratch pages, there are 15 pages to the exam.
Please verify that you have all 15 pages. You are allowed to tear off the last three (rules and
scratch) if this makes it easier to take the test.
Question Total points Score
1 10
2 9
3 16
4 19
5 12
6 20
7 8
8 6
SUBTOTAL 100
Extra Credit 12
TOTAL 112
University of Illinois at Urbana-Champaign Netid:
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

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

CS421 Summer 2006 Midterm

CS 421 — Introduction to Programming Languages

Summer Term 2006

11:30am–12:45pm, Thursday, June 29, 2006

The total time for this exam is 75 minutes.

Print your name and netid below. Also write your name at the bottom of each subsequent page.

Name:

Netid:

  • This is a closed-book exam. You are allowed one 3 inch by 5 inch card of notes prepared by yourself. You may write on both sides of the card. This card is not to be shared. All other materials, besides pens, pencils, and erasers, are to be put away.
  • Do not share anything with other students. Do not talk to other students. Do not look at another student’s exam. Also, be careful to 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 must document your assump- tions about what the question means. We are not allowed to answer questions about the exam in class, and obviously cannot answer these questions for students taking the exam elsewhere.
  • Including this cover sheet, rules at the end, and scratch pages, there are 15 pages to the exam. Please verify that you have all 15 pages. You are allowed to tear off the last three (rules and scratch) if this makes it easier to take the test.

Question Total points Score 1 10 2 9 3 16 4 19 5 12 6 20 7 8 8 6 SUBTOTAL 100 Extra Credit 12 TOTAL 112

  1. Short Answer Questions (10 points):

(a) In the following, state TRUE or FALSE for each part separately; each part is 1 point (write your answers to the left): A statically-typed language: i. assigns types to all expressions at compile time ii. may require run-time checks to guarantee type errors do not occur iii. always has a strong type system iv. always requires explicit type declarations

(b) In the following, state TRUE or FALSE for each part separately; each part is 1 point (write your answers to the left): If a language is strongly typed: i. an operation that could produce a type error may be detected at compile-time or at run-time ii. the use of type inference to determine types of expressions is required iii. the language must be statically typed iv. the language must be dynamically typed

(c) (2 points) What value is produced by the following program? let x = 10 in let y = 5 + x in let f y = y + x in let x = 20 + y in f x;;

  1. Higher-Order Functions and Data Types (16 points):

Given the following OCaml datatype:

type int_pair = Empty | LPair of int_pair * int | RPair of int * int_pair;;

(a) (6 points) Write a recursive function fold pair: val fold_pair : (int -> ’a -> ’a) -> (int -> ’a -> ’a) -> ’a -> int_pair -> ’a = that folds a function over an int pair. It should take a function that works on LPair (typed between the first set of parens in the type signature for fold pair), a function that works on RPair (typed between the second set of parens in the type signature for fold pair), and an identity for Empty, as well as the int pair, returning the value of the fold computation.

(b) (5 points) Write a function sum rpair, using fold pair, that will calculate the sum using only the integer value stored in RPair items – in other words, the int that is part of an LPair will be disregarded for the sum (but the int pair that is part of the LPair still needs to be processed, since it may contain RPair items that will contribute to the sum).

(c) (5 points) Write a function flip lpair, using fold pair, that will turn an LPair into an RPair, but will leave RPair and Empty items as they are.

  1. Type Derivations (19 points): Below is an outline of the type derivation of the following expression:

let f = (fun x −> (g x)) in f 10

Please complete the derivation by giving the results to go in the numbered blanks. Put your answers next to the corresponding numbers below the type derivation outline.

(#14) ⊢ (#15) : (#16) (#17) ⊢ (#18) : (#19) (#11) ⊢ (#12) : (#13) (#1) ⊢ (fun x −> (g x)) : (#2)

(#5) ⊢ (#6) : (#7) (#8) ⊢ (#9) : (#10) (#3) ⊢ f 10 : (#4) {g : int → bool} ⊢ let f = (fun x −> (g x)) in f 10 : bool

  1. Lambda Calculus (20 points):

(a) (5 points) Use β-reduction with a lazy evaluation order to reduce the following term as fully as possible: (λb c d.b c d)(λx y.x)(λs.s)((λf.f f)(λt.t))

(b) (5 points) Use β-reduction with an eager evaluation order to reduce the following term as fully as possible (note: this is the same term as above): (λb c d.b c d)(λx y.x)(λs.s)((λf.f f)(λt.t))

(c) (5 points) Use β-reduction to reduce this term as fully as possible. If you need α- conversion, show explicitly where it is needed. Use an eager evaluation order. (λa b.b b a)((λx y.y)(λw.a w)(λv.b v))(λt.t)

(d) (5 points) Recall our definition of pairs in the λ calculus – a pair can be represented as a term λx.x a b where x is the constructor tag (here we only have one constructor, so we only need one tag) and a and b are the two elements in the pair. The pair constructor abstraction is then λa b x.x a b, which will take the two elements of the pair and “plug them in” to the correct spots. Using this definition of pairs, provide a λ-abstraction that, given a pair and a term, will return a new pair where the first element is the same as that of the input pair and the second element is the input term. In other words, given pair (a, b) and term t, the pair (a, t) should be returned.

  1. Finite Automata (6 points, 1 point each): For the given finite automata over the alphabet {a, b}, several possible input strings are provided. For each string, determine if it is accepted by the automata. If so, circle it. If not, cross it out.

a ǫ (^) a

a

b (^) ǫ

(a) a (b) baab (c) aaaab (d) b (e) aaaaa (f) ba

  1. Extra Credit (12 points): The following type may be used to represent terms of the lambda calculus as abstract syntax trees.

type lterm = Var of string (* x ) | Abs of string * lterm ( \lambda x. t ) | App of lterm * lterm ( t t’ *);;

(a) (3 points) Write a function bound vars which, given a term of type lterm, returns a list of strings (string list) containing all the names of variables having a bound occurrence in the term. It is acceptable if a name appears in the list more than once.

(b) (4 points) Write a function gen church numeral which, given a non-negative integer (type int), returns an lterm representing the equivalent Church Numeral. Remember our definitions: 0 ≡ λf x.x, 1 ≡ λf x.f x, and in general Suc n ≡ λf x.f(n f x).

Rules for type derivations:

Constants

⊢ n : int (assuming n is an int)

⊢ true : bool

⊢ false : bool

Variables

Γ ⊢ x : τ if(x : τ ) ∈ Γ

Arithmetic Operators Γ ⊢ e 1 : int Γ ⊢ e 2 : int Γ ⊢ e 1 ⊕ e 2 : int

(⊕ ∈ {+, −, ∗, /, · · ·})

Relational Operators Γ ⊢ e 1 : int Γ ⊢ e 2 : int Γ ⊢ e 1 ∼ e 2 : bool

(∼ ∈ {<, >, ≤, ≥, =, 6 =, · · ·})

Booleans Γ ⊢ e 1 : bool Γ ⊢ e 2 : bool Γ ⊢ e 1 && e 2 : bool Γ ⊢ e 1 : bool Γ ⊢ e 2 : bool Γ ⊢ e 1 || e 2 : bool Γ ⊢ e 1 : bool Γ ⊢! e 1 : bool If Γ ⊢ e 1 : bool Γ ⊢ e 2 : τ Γ ⊢ e 3 : τ Γ ⊢ if e 1 then e 2 else e 3 : τ Function Abstraction Γ ∪ [x : τ 1 ] ⊢ e : τ 2 Γ ⊢ fun x−>e : τ 1 → τ 2 Function Application Γ ⊢ e 1 : τ 1 → τ 2 Γ ⊢ e 2 : τ 1 Γ ⊢ e 1 e 2 : τ 2 Let Γ ⊢ e 1 : τ Γ ∪ [x : τ ] ⊢ e 2 : τ ′ Γ ⊢ let x = e 1 in e 2 : τ ′ Letrec Γ ∪ [x : τ ] ⊢ e 1 : τ Γ ∪ [x : τ ] ⊢ e 2 : τ ′ Γ ⊢ let rec x = e 1 in e 2 : τ ′