Fall 2011 Final Examination Prasad
1. Scheme: Recursive Definition (5 + 4 / 3 + 4 + 2 pts)
Consider the list functions computed by the following Standard (R5RS) Scheme definitions.
(define (mystery lis)
(cond ((null? lis)
'())
((symbol? (car lis)) (cons 's (mystery (cdr lis))))
((number? (car lis)) (cons 'n (mystery (cdr lis))))
((list? (car lis))
(cons (mystery (car lis))
(mystery (cdr lis)) ) )
(else (mystery (cdr lis)) )
)
)
What values are returned for the following calls: (mystery '(a -21)) and (mystery '(a (b 2) 3))? Trace the computation carefully by displaying all the calls to mystery.
Explain clearly the list function computed by the above
Scheme definition.
(Additional problem for CS680 Students)
Write a Scheme function eliminate that takes a nested list of symbols and
numbers, and eliminates all the symbols and nested lists, return just the flat list of top-level numbers.
(eliminate '(a (1) () (b 2) 3 4)) =returns=> (3 4)
2. PL Concepts (5 * 3 / 3 * 5 pts)
3. Java : Polymorphism and Dynamic Binding (5 pts)
Does the following program compile without any errors? If not, correct the program "minimally" before proceeding further.
Write the output of executing the (possibly corrected) Java program indicating any run-time exceptions it may generate. (Hint: Focus on the declared type of a variable and the class associated with the object to which the variable holds a reference.)
class P {
String f(P
x) { return (" abc ");}
String f(Q
x) { return (" def ");}
}
class Q extends P {
String f(P
x) { return (" ghi ");}
String f(Q
x) { return (" jkl ");}
}
class
Final {
public
static void main(String[] args) {
P[]
arr = {new P(), new Q(), null};
System.out.println(arr[0].f(arr[1]));
System.out.println(arr[1].f(arr[0]));
System.out.println(arr[1].f(arr[1]));
System.out.println(arr[1].f(arr[2]));
System.out.println(arr[2].f(arr[2]));
}
}
4 Top-down Parser (4 + (3 + 4) pts)
Study the following Java program carefully and answer the following questions.
x - y
x - x - y