Programming Paradigms 2005/2006
Final Examination
January 24th, 2006
NAME:
• Please read all instructions carefully before start answering. • The exam will be 120 minutes in length.
• The exam is open book; any written materials may be used. • Answer all questions on the exam paper itself; the backs of the
pages may be used if needed.
• The exam has 7 problems, with varying number of points for each problem, for a total of 100 points
1. Fundamentals [10 pontos]
Consider the following BNF gramar: hpopi ::= [hbopi,hpopi ]| hbopi hbopi ::=hboopi | ( hpopi ) hboopi ::= x|y|z
For each of the following strings, identify the syntactic categories to which it belongs, showing the sequence of derivations from production hpopi.
a) z
b) (x)
c) [y]
d) [(x),y]
2. Type Systems and Type Inference [15 points]
Consider the following ML function: fun append(nil, l) = l
| append(x::l, m) = append(l, m)
a) Write one or two sentences to explain succinctly and informally whyappendhas the type you give.
b) Following the steps of the ML type-inference algorithm, write the parse graph for the first clause of functionappend.
c) This function is intended to append one list onto another. However it has a bug. How might knowing the type of this function help the programmer to find the bug
3. Scope, Functions and Store Management [15 points]
Consider the following statically scoped ML expression: val x= 5;
fun f(y) = (x+y) -2;
fun g(h) = let x = 7 in h(x) end; let val x = 10 in g(f) end;
a) Fill in the missing information in the following illustration of the runtime stack after the call to h inside the body of g. Remember that function values are represented by closures and that a closure is a pair consisting of an environment (pointer to an activation record) and compiled code1.
Activation Records Closures Compiled Code
(1) access link ( 0 ) x
(2) access link ( 1 )
f •
(3) access link ( ) h( ), • i
g • |code for f |
(4) access link ( ) h( ), • i x
(5) g(f) access link ( )
h • |code for g |
x
(6) h(x) access link ( ) y
b) What is the value of this expression? Why?
4. Control in Sequential Languages [15 points]
Binding name occurrences to declarations may occur at compilation time (lexical scope) or at runtime (dynamic scope).
Consider the following ML program: let y = 11
in let f x = x + y in let y = 22
in f 3 end end
a) Explain what is lexical scope. Identify the binding of ywhich is used in fwith lexical scope and evaluate the result of the call f 3.
b) Explain what is dynamic scope. Identify the binding of y which is used in fwith dynamic scope and evaluate the result of the call f 3.
a) Without using if-then-else write in C++the class hierarchy of Tree,Leaf and Node, such
that the following code works (using C++syntax):
Tree * left = new Leaf(1);
Tree * right = new Node(new Leaf(2), new Leaf(3)); Tree * root = new Node(left, right);
int h = root->sum(); where sum()is a virtual function.
6. Portability and Safety: Java [15 points]
Consider the following Java program in which an exception may be raised in procedureproc A at the point indicated in the program.
class X {
static void proc_A() { try {
System.out.println ("begin proc_A"); /*
throw NullPointerException, RuntimeException, Exception, etc */
} catch (NullPointerException e) {
System.out.println("handler in proc_A"); } finally {
System.out.println ("end proc_A"); }
static void proc_B() { try {
System.out.println("begin proc_B"); proc_A();
} catch (RuntimeException e) {
System.out.println("handler in proc_B"); } finally {
System.out.println("end proc_B"); }
}
public static void main (String args[]) { try {
System.out.println("begin main"); proc_B();
} catch (Exception e) {
System.out.println("handler in main"); } finally {
System.out.println("end main"); }
} }
Describe the execution of the entire program for each of the following scenarios by showing the output and saying how the program terminates.
a) Suppose procedure proc Araises no exception.
b) Procedure proc Araises the exceptionRuntimeException.
7. Logic Programming [15 points]
Consider the following Prolog database: a(X) :- b(X),c(X).
a(X) :- d. a(X) :- e(X). b(1).
b(2). c(2). c(3). e(4).
a) Draw the Prolog search tree and identify the answer of the interpreter when all solutions of the following query are evaluated.
a(Z).
b) What is the result when the second rule is changed, by introducing a cut, to: a(X) :- !, d.
c) What is the result when the second rule is changed, by introducing a cut, to: a(X) :- d, !.