Principles of programming languages

 

Spring 2006

 

Examination paper (sample)

 

This is a closed book written exam.

The answers can be given in Dutch or English.

The final grade is calculated as (Q1+Q2+Q3+..+10)/10

A pass for this exam is valid only in combination with a fulfilled assignment

Credits:

 

Q1

Q2

Q3

Q4

Q5

Q6

Q7

Q8

Q9

SQi

a)

3

4

5

10

10

5

5

5

5

 

b)

2

4

5

 

 

5

3

6

5

 

c)

2

 

 

 

 

 

3

 

 

 

d)

3

 

 

 

 

 

 

 

 

 

Totaal

10

8

10

10

10

10

11

11

10

90

 

 

1. Syntax and semantics [10p]

 

Given the following BNF grammar:

<exp> ::= <term> + <exp> | <term> - <exp> | <term>
<term> ::= <factor> * <term> | <factor> / <term> | <factor>
<factor> ::= ( <exp> ) | a | b | c | d | 1 | 2 | 3 | 4

 

 

a) Build a parse tree for the expression a-b*(c+d). [3p]

b) Show on the tree what a production is, a non-terminal symbol, a token. [2p]

c) What is the value of 1-2*3 in the language defined by the grammar, if expressions are evaluated in the order implied by the parse tree? [2p]

d) Which of the following expressions can NOT be parsed by the grammar? [3]

((a))

a+b+(c+d)

a+b*(-1)

a+b*c

 

 

 

2. Language systems [8 p]

 

a) List the steps in the classical sequence of a language system.[4 p]

b) What is the binding time for the meaning of the keyword while in Java? [4p]

 

 

3. Types [10p]

 

a) Which of the following is an example of coercion [5p]?

 

A. The way the expression a+b is evaluated in C, when a is an int and b is a float.

B. The way the length function in ML works on any type of the form a list .

C. The way the + operator in C applies to pairs of integers and also to pairs of real numbers

 

b) What is a type annotation? Give an example in ML. [5p]

4. Functional programming [10 p]

 

Explain what pattern matching is in ML and show how this applies for the following code snippet]:

 

fun f nil = 0

| f (first::rest) = first + f rest

 

 

f [1,3,5,7]

 

 

5. Memory management [10p]

 

Comment on the trade-off between static and dynamic activation record allocation.

 

6. Object oriented programming [10p]

 

Consider this C++ program :

 

#include <iostream>

using namespace std;

 

class A {

public:

void print() {cout << "A";}

};

 

class B : public A{

public:

void print() {cout << "B";}

};

 

int main()

{

A* p ;

p = new A() ;

p->print() ;

 

p = new B() ;

p->print() ;

return 0;

}

 

 

 

 

7. Exceptions [11p]

 

a) Name and explain the components of a Java exception handling mechanism [5p]

b) What is the output of this Java code? [3 p]

 

1 System.out.print("1");
2 try {
3 System.out.print("2");
4 if (true) throw new Exception();
5 System.out.print("3");
6 }
7 catch (Exception e) {
8 System.out.print("4");
9 }
10 finally {
11 System.out.print("5");
12 }
13 System.out.println("6");

 

c) What happens if we change in line 7 catch (Exception e) with catch (Throwable e)? [3 p]

Q8. Logic programming (Prolog) [11 p]

 

Given the facts:

 

loves(john, jane).

loves(bill, jane).

loves(james,jane).

loves(mary, bill).

loves(jane, james).

 

And the rule:

 

goodmatch(X,Y) :- loves(X,Y), loves(Y,X).

 

a)      What is unification? Show how unification works in answering the question [5p]

 

?- loves (mary, X)

 

b)      Show the backtracking involved in answering the question: [6p]

 

?- goodmatch(A,B)

 

 

Q9. Scripting languages (Python) [10p]

 

a) Why is Python considered a very high level programming language? Support your answer with examples [5p].

 

b) Explain what this Python function does and give an example in which this function is used properly [5p].

 

def func (*args):

res = args[0]

for arg in args[1:]:

if arg < res:

res = arg

return res