Summer 2011 Final Examination Prasad
<expr> -> <term> { + <term> }class Test {
static double f(double a, int i){
return <expr>;
}
public static void main(String[] args){
System.out.println(f(1,2.0));
}
}
2. Java : Polymorphism and Dynamic Binding (1 + 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 referred to by the variable.)
class A {
String f(A x) { return (" AA ");}
String f(B x) { return (" AB ");}
}
class B extends A {
String f(A x) { return (" BA ");}
String f(B x) { return (" BB ");}
}
class Final {
public static void main(String[] args) {
A[] arr = {new A(), new B(), null};
System.out.println(arr[0].f(arr[0]));
System.out.println(arr[1].f(arr[0]));
System.out.println(arr[1].f(arr[1]));
System.out.println(arr[0].f(arr[2]));
System.out.println(arr[2].f(arr[0]));
}
}