Instituto Superior Técnico
Programação Avançada
Second Exam – 13/7/2010
Write your number on every page. Your answers should not be longer than the available space. You can
use the other side of the page for drafts. The exam has 5 pages and the duration is 2.0 horas. The grade for
each question is written in parenthesis. Good luck.
1. (1.0) What is reflection? Which are the degrees of reflection that you know? Explain and show
Reflexão é a capacidade que um sistema computacional tem de manipular uma representação da sua estrutura e comportamento durante a
sua própria execução.
Existem dois graus de Reflexão:
• A Introspecção é a capacidade de um sistema de observar a sua
própria estrutura e comportamento. Por exemplo, saber “Quantos
parâmetros tem a função foo?” é introspecção.
• A Intercessão é a capacidade de um sistema de modificar a sua
própria estrutura e comportamento. Por exemplo, “Mudar a classe
desta instância para Bar” é intercessão.
2. (1.0) In order to allow reflection, the Java language reified several concepts. Describe three of those
Java reificou o conceito de classe, o conceito de método e o conceito de
atributo ( field).
3. (1.0) What is meta-programming? Explain.
A meta-programação é a escrita de programas que produzem outros
programas. Um meta-programa manipula uma estrutura de dados que
representa outro programa.
4. (2.0) Some people say that the interfaces of the Java language can be used to implement multiple
inheritance. Do you agree? Explain your answer.
5. (2.0) In CLOS, what is the difference between a generic function, a primary method, and a auxiliary
method? Explain.
A função genérica é uma função que pode ser particularizada para diferentes tipos de argumentos (ou para diferentes argumentos). Para
além disso, ela armazena todos os métodos que a especializam. Um
método primário é o método que defina o comportamento fundamental do método efectivo. Um método auxiliar é um método que modifica
esse comportamento fundamental.
6. (1.0) The gensym function exists since a long time in almost all Lisp dialects. What does it do?
Where is it used and for what? Explain.
A função gensym devolve um novo sı́mbolo único de cada vez que é
invocada. É usada fundamentalmente para a definição de macros de
modo a evitar problemas de múltipla avaliação e de captura indevida
de bindings.
7. (1.0) Consider the following function defined in Common Lisp:
(defun foo (x f)
(funcall f (bar #’g x)))
Rewrite the previous definition but using the Scheme language.
(define (foo x f)
(f (bar g x)))
8. (3.0) We want to develop a mechanism for instrumenting Java programs so that we can measure
their performance. In particular, we want to know, for each method, how much time was spend
in its execution.
Suggest an intercession mechanism based on Javassist that allows us to measure the time spent
with the invocation of each method of a given class. Your proposal should provide a way for
the programmer to insert an instruction that causes the program to write the current state of the
instrumentation, namely, the time spent on each method up to that moment. It is not necessary
to present the implementation of the mechanism but include all the necessary information that
is relevant for someone else to implement the mechanism you propose.
9. (2.0) The Pascal language allows us to define functions inside other functions but it doesn’t allow
us to return functions as results or to store them in variables. On the other hand, the C language
allows us to return functions as results and to store them in variables but it doesn’t allow us to
define functions inside other functions.
(a) (1.0) What is the problem that justifies these limitations of Pascal and C? Explain.
(b) (1.0) What do you suggest that we do in a meta-circular evaluator in order to eliminate those
limitations? Explain.
Associar a cada função o ambiente léxico em que foi definida.
10. (1.0) Some programing languages define an evaluation order for the operands of an operator (for
example, from left to right) while others leave that order unspecified. In what conditions do
you think one of the options is preferable to the other? Explain.
Quando uma language promove o uso de efeitos secundários, é normalmente preferı́vel que o programador possa contar com uma ordem
de avaliação da esquerda para a direita.
11. (2.0) Which are the problems that aspect oriented programming (AOP) wants to solve? How does
AOP address them? Explain.
12. (1.0) Describe, in your own words, the effect of the following aspect in AspectJ:
aspect ShapeListening {
private Vector<Screen> Shape.listeners = new Vector<Screen>();
pointcut changeShape(Shape s):
call(void Shape+.set*(..)) && target(s);
after(Shape sh): changeShape(sh) {
for(Screen sc : sh.listeners) {
13. (2.0) A Pythagorean triple consists of three positive integers i, j, and k, such that i2 +j 2 = k 2 . Using
the non-deterministic operators amb e fail, define a function (and all auxiliary functions that
you want) that has a parameter n and that non-deterministically computes a Pythagorean triple
such that i ≤ j ≤ k ≤ n.
(define (an-integer-between a b)
(if (> a b)
(amb a
(an-integer-between (+ a 1) b))))
(define (a-pythagorean-triple n)
(let ((i (an-integer-between 1 n)))
(let ((j (an-integer-between i n)))
(let ((k (an-integer-between j n)))
(if (= (+ (* i i) (* j j)) (* k k))
(list i j k)

English Exam 2-2010