Master of Sciences in Informatics Engineering Programming Paradigms 2005/2006 Final Examination January 24 th, 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 Problem 1 2 3 4 5 6 7 Total Max. Points 10 15 15 15 15 15 15 100 Points João Correia Lopes 1
1. Fundamentals [10 pontos] Consider the following BNF gramar: pop ::= [ bop, pop ] bop bop ::= boop ( pop ) boop ::= x y z FEUP/MEI/PP/2005-06 For each of the following strings, identify the syntactic categories to which it belongs, showing the sequence of derivations from production pop. a) z b) (x) c) [y] d) [(x),y] e) [(x),[y,x]] João Correia Lopes Página 2 de 8
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) FEUP/MEI/PP/2005-06 a) Write one or two sentences to explain succinctly and informally why append has the type you give. b) Following the steps of the ML type-inference algorithm, write the parse graph for the first clause of function append. João Correia Lopes Página 3 de 8
FEUP/MEI/PP/2005-06 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 code 1. Activation Records Closures Compiled Code (1) access link ( 0 ) x (2) access link ( 1 ) f (3) access link ( ) ( ), g code for f (4) access link ( ) ( ), 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? 1 You should use the same conventions as in Mitchell s book. João Correia Lopes Página 4 de 8
4. Control in Sequential Languages [15 points] FEUP/MEI/PP/2005-06 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 y which is used in f with 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 f with dynamic scope and evaluate the result of the call f 3. 5. Object-Oriented Languages [15 points] Consider the following ML program: (* A tree is either a Leaf containing an integer value or an interior Node with two subtrees. *) datatype Tree = Leaf of int Node of Tree * Tree (* Return the sum of all the integers stored in Leaf nodes. *) fun sum (Leaf i) = i sum (Node (l, r)) = sum l + sum r João Correia Lopes Página 5 de 8
FEUP/MEI/PP/2005-06 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 procedure proc 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"); João Correia Lopes Página 6 de 8
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"); FEUP/MEI/PP/2005-06 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 A raises no exception. b) Procedure proc A raises the exception RuntimeException. c) Procedure proc A raises the exception NullPointerException. d) Procedure proc A raises the exception Exception. João Correia Lopes Página 7 de 8
FEUP/MEI/PP/2005-06 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,!. The End. João Correia Lopes Página 8 de 8