CS164 Midterm Exam, Fall 2007: Solutions and Questions, Exams of Programming Languages

The second midterm exam for cs164, a computer science course, held in fall 2007. The exam consists of four problems, each with multiple parts. The problems cover topics such as activation records, visitors in object-oriented programming, closures, and syntax-directed translation. Students are required to write their answers in the space provided on the exam and are given a specific time limit.

Typology: Exams

2012/2013

Uploaded on 04/02/2013

shailaja_987c
shailaja_987c 🇮🇳

4.3

(34)

217 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
P a g e | 1
Second Midterm Exam
CS164, Fall 2007
Nov 8, 2007
Please read all instructions (including these) carefully.
Write your name, login, and SID.
No electronic devices are allowed, including cell phones used as watches.
Silence your cell phones and place them in your bag.
The exam is closed book, but you may refer to one (1) page of handwritten notes.
Solutions will be graded on correctness and clarity. Each problem has a
relatively simple and straightforward solution. Partial solutions will be graded
for partial credit.
There are 6 pages in this exam and 4 questions, each with multiple parts. If you
get stuck on a question move on and come back to it later.
You have 1 hour and 20 minutes to work on the exam.
Please write your answers in the space provided on the exam, and clearly mark
your solutions. You may use the backs of the exam pages as scratch paper. Do
not use any additional scratch paper.
LOGIN: _______________________
NAME: _______________________
SID: _______________________
Problem
Max points
Points
1
20
2
30
3
30
4
20
TOTAL
100
pf3
pf4
pf5

Partial preview of the text

Download CS164 Midterm Exam, Fall 2007: Solutions and Questions and more Exams Programming Languages in PDF only on Docsity!

Second Midterm Exam

CS164, Fall 2007

Nov 8, 2007

 Please read all instructions (including these) carefully.

 Write your name, login, and SID.

 No electronic devices are allowed, including cell phones used as watches.

 Silence your cell phones and place them in your bag.

 The exam is closed book, but you may refer to one (1) page of handwritten notes.

 Solutions will be graded on correctness and clarity. Each problem has a

relatively simple and straightforward solution. Partial solutions will be graded

for partial credit.

 There are 6 pages in this exam and 4 questions, each with multiple parts. If you

get stuck on a question move on and come back to it later.

 You have 1 hour and 20 minutes to work on the exam.

 Please write your answers in the space provided on the exam, and clearly mark

your solutions. You may use the backs of the exam pages as scratch paper. Do

not use any additional scratch paper.

LOGIN: _______________________

NAME: _______________________

SID: _______________________

Problem Max points Points

TOTAL 100

Problem 1: Activation Records [20 points] Part 1 [5 points]: Even a statically scoped language needs a dynamic link in its activation record. What purpose does the dynamic link serve? Answer: _____________________________________________ Part 2 [10 points]: Draw the configuration of the call stack at the point when this program reaches the print statement. Include in your figure:  Dynamic links  Static links  Arguments  Local variables You can ignore other content of the activation record. x = 0 function f() local x = 1 function g(n) local y = n if n>0 then g(n-1) else print(y*x) end end g(3) end f() Your answer: This is the stack. It grows upwards from the bottom. args locals SL DL

Problem 3: Closures [30 points] Part 1 [5 points]: What does the following program output? Answer: __, __, __, __ function f() local x = 1; return function () x = x+1; return x end end i1 = f() i2 = f() print(i1(), i2(), i1(), i2()) Part 2 [5 points]: How many copies of variable x are created during the execution of the above program? Answer: __ Part 3 [5 points]: Indicate (by drawing a ‘*’ sign) where in the code can these variable(s) be deallocated. Indicate the earliest point(s) where deallocation is safe. Continues on next page

Part 4 [15 points]: The Lua code below implements a list module (you have seen a similar code in the lecture). Your goal is to implement the function iterator() so that it can be used to iterate over the values of the list exactly as shown below. Use closures to implement the iterator functionality. Assume that the list is not modified while an iterator operates on it, but two iterators may simultaneously iterate over the same list. List = {} function List.new () return {first=0, last=0} end function List.push(list,value) list[list.first] = value list.first = list.first+ end function List.remove(list,value) if list.last >= list.first then return nil end local last = list.last list.last = list.last + 1 return list[last] end myList = List.new() List.push(myList, 1) List.push(myList, 2) List.push(myList, 3) next = List.iterator(myList) while true do element = next() if element == nil then break end print(element) -- outputs 1 2 3 end