7 Questions on Programming and Problem Solving I - Midterm 2 | CS 421, Exams of Computer Science

Material Type: Exam; Professor: Kamin; Class: Progrmg Languages & Compilers; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Spring 2010;

Typology: Exams

Pre 2010

Uploaded on 12/08/2010

agarwa15
agarwa15 🇺🇸

2 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 421 Spring 2010 Midterm 2
Wednesday, April 7, 2010
Name
NetID
You have 75 minutes to complete this exam
This is a closed bo ok exam.
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, seek clarification from one of the
TAs. You must use a whisper, or write your question out.
Including this cover sheet, there are 11 pages to the exam. Please verify that you have all 11
pages.
Please write your name and NetID in the spaces above, and at the top of every page.
Question Value Score
1 8
2 12
3 14 + 5 XC
4 15
5 22 + 5 XC
6 15
7 14
Total 100 + 10
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download 7 Questions on Programming and Problem Solving I - Midterm 2 | CS 421 and more Exams Computer Science in PDF only on Docsity!

CS 421 Spring 2010 Midterm 2

Wednesday, April 7, 2010

Name

NetID

  • You have 75 minutes to complete this exam
  • This is a closed book exam.
  • 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, seek clarification from one of the TAs. You must use a whisper, or write your question out.
  • Including this cover sheet, there are 11 pages to the exam. Please verify that you have all 11 pages.
  • Please write your name and NetID in the spaces above, and at the top of every page.

Question Value Score

3 14 + 5 XC

5 22 + 5 XC

Total 100 + 10

  1. (8 pts) Fill in the blanks below, giving the names of the various parts of a compiler. (Re- call that the cylinders represent data and the boxes represent actions (i.e. functions).)

A front-end

B back-end

C lexer

D parser

E AST

F symbol table

G optimization

H code generation

  1. (14 pts) In class, we gave the following translation schemes for translating source programs into an intermediate representation (IR). All but the first take an AST (expression or state- ment) to a sequence of IR instructions. [e] : translate expression e to IR; returns pair (IR instruction list, location of value) [S] : translate statement S to IR [e]x : translate expression e to code that stores value of e in variable x [S]L : translate statement S in context of a loop or switch statement, where L is the target of a break statement [e]Lt,Lf : translate expression e to code that branches to Lt if e is true, or Lf otherwise (the short-circuit evaluation scheme) The instructions in our intermediate representation were: x = n; x = y; x = y + z (for any operation +); JUMP L; CJUMP x, L1, L2; and x = LOADIND y.

(a) Give the following translations. (You may use functions getloc() and getlabel() to get fresh memory locations and fresh instruction labels, respectively.) i. [e 1 + e 2 ]

let t1, t2, t3 = getloc() in [e 1 ]t [e 2 ]t t3 = t1 + t

ii. [e 1? e 2 : e 3 ]x (for full credit, use the short-circuit scheme for e 1 )

[e 1 ]L1,L L1: [e 2 ]x JUMP L L2: [e 3 ]x L3:

iii. [e 1 && !e 2 ]Lt,Lf (e 2 should not be evaluated if e 1 is false)

[e 1 ]L1,Lf L1: [e 2 ]Lf,Lt

(b) (5 pts extra credit) Give IR code for a for loop. A for loop has the form “for(S 1 ; e; S 2 ) S 3 ”, where S 1 is executed before the loop begins, the loop ends when e evaluates to false, S 2 is executed at the end of each iteration of the loop, and S 3 is the loop body. For full credit, use the short-circuit scheme for e.

[S 1 ]

JUMP L

L1: [S 3 ]L

[S 2 ]

L2: [e]L1,L L3:

  1. (22 pts)

(a) Give the type of the following function: fun f -> fun g -> fun x -> g (f x) x

(α → β) → (β → α → γ) → α → γ

(b) Write an OCaml function update such that update f a b is a function that returns b when given a as input but otherwise behaves the same as f.

let update f a b = fun x -> if x = a then b else f x

(c) Write an OCaml function double that duplicates each element of a list, using fold right instead of explicit recursion. For example, double [1; 2; 3] = [1; 1; 2; 2; 3; 3]. Remember that fold right has type (α - > β - > β) -> α list -> β - > β.

let double lis = fold right (fun x y -> x :: x :: y) lis []

(d) Write an OCaml function sum pairs that takes a list of pairs and returns a list containing the sum of the elements of each pair, using map instead of explicit recursion. For example, sum pairs [(1, 2); (3, 4); (5, 6)] = [3; 7; 11].

let sum pairs = map (fun (x, y) -> x + y)

(e) (5 pts extra credit) Write an OCaml function maxf that takes a function f and a list lst and returns a pair (max, index), where max is the largest value produced by applying f to an element of lst, and index is the index in lst of the element x such that f x = max, where the first element of the list has index 0. If there are multiple such elements, you may return the index of any one of them. For example, maxf (fun x -> x + 2) [1; 2; 3] = (5, 2). You may assume that lst is never empty. You may also assume that f takes elements of lst and returns only positive integers. Your function should use fold right instead of explicit recursion.

let maxf f lst = fold right (fun x (m, i) -> if f x > m then (f x, 0) else (m, i+1)) lst (0,0)

  1. (14 pts) Write a function object in Java for the OCaml function apply pos, defined as follows:

apply pos f lst = map (fun x -> if x > 0 then f x else x) lst For simplicity, we assume that lst is a list of integers. As in the OCaml code, your Java solution should call Map.map, which is given here:

interface IntFun { int apply (int n); }

class Map { static int[] map (IntFun f, int lis[]) { int lis2[] = new int[lis.length]; for(int i = 0; i < lis.length; i++) lis2[i] = f.apply(lis[i]); return lis2; } }

class Apply_Pos { static int[] apply_pos (final IntFun f, int lis[]) { // complete this method IntFun g = new IntFun(){ int apply(int n){ return n > 0? f.apply(n) : n; } }; return Map.map(g, lis); } }

APL Reference

Operation Expression Value

Sample data A ; a 2,3-matrix

V ; a 3-vector 2 4 6 C ; a logical 2-vector 1 0 D ; a logical 3-vector 1 0 1 Arithmetic A *@ A

V -@ (newint 1) 1 3 5 Relational A >@ (newint 4)

Reduction !+ V 12 maxR A 3 6 Compression D % V 2 6 C % A 1 2 3 (a 1,3-matrix) Shape shape A 2 3 Ravelling ravel A 1 2 3 4 5 6 ravel (newint 1) 1 Restructuring rho (shape A) V

rho (shape V) C 1 0 1 Catenation A ^@ C 1 2 3 4 5 6 1 0 Index generation indx (newint 5) 1 2 3 4 5

Transposition trans A

Subscripting V @@ (indx (newint 2)) 2 4 A @@ (newint 1) 1 2 3 (a 1,3-matrix) (trans A) @@ (indx (newint 2))