You have 150 minutes to answer these questions. Please read all questions before you start. This is a closed-book exam. Please remember that you are bound by the CU honor code.
[5 points] A language for arithmetic expressions, where the only arithmetic operators are "+" and "*" and the only operands are the numbers 0 through 9. For example, 9+2, 9*3+5, and 9 would all be in the language.
[5 points] Is your grammar for arithmetic expressions ambiguous?
Explain clearly why or why not.
Consider the following code fragment in Java syntax:
try {
... /* Try block */
}
catch (E1 e) { ... /* Catch block */ }
finally {... /* Finally block */ }
print("After try"); /* print does not throw any exceptions */
[2 points] Explain broadly the purpose of the "finally" block in a try-catch-finally statement.
For each of the following cases, indicate what code will be executed, in what order, and what exceptions, if any, will be thrown, and in what order. A block "terminates normally" if it does not throw any exceptions and does not execute a "return" statement.
[2 points] All the blocks (if executed) will terminate normally.
[2 points] The try block throws an exception of type E1, the catch block (if executed) will throw an exception of type E2, and the finally block (if executed) will terminate normally.
[2 points] The try block executes a "return" statement, the catch block and finally blocks (if executed) terminate normally.
[2 points] The try block terminates normally, the catch block (if executed) will throw an exception of type E2, and the finally block (if executed) will throw an exception of type E3.
Consider the following two SML functions:
fun f1(nil,v) = nil
| f1(h::t,v) = if v=h then f1(t,v) else h::f1(t,v);
fun f2(a,0)
= a
| f2(h::t,n) = f2(t,n-1)
| f2(nil,n) = nil;
[5 points] Clearly describe what function f1 does
[3 points] Give the type that will be inferred for f1 by the SML compiler
[5 points] Clearly describe what function f2 does
[3 points] Give that type that will be inferred for f2 by the SML compiler