Name (Last, First):, Sign your name: Student ID Number (last six digits, e.g., 12-3456): Grade: /100 points This exam consists of 4 questions on 10 sides, printed on both sides of 5 pages; be sure you have the entire exam before starting. The point value of each question is indicated at its beginning; the entire exam has 100 points. Individual parts of a multipart question are generally assigned approximately the same point value; exceptions are noted. This quiz is open notes and text, but closed neighbor. Read through all questions first and pace yourself. Be concise and indicate clearly what your answer is. Presentation and simplicity of your answers may affect your grade. Answer each question in the space following the question. If you find it necessary to continue an answer elsewhere, indicate clearly the location of its continuation and label its continuation with the question number. If you are asked to explain in X words and your answer exceeds X words, then (1) we will consider only the first X words in grading your answer and (2) your answer will lose 20% of the points it would have received. Youneed to show work only where requested. Note well: On questions about HW4, assume a working HW4 implementation using standard techniques described in the handout, given code, lecture, and discussion. If you did something different and it s relevant to your answer to a question, be sure you describe what you did. 1
Question 1. (25points ) (a) (8 points) Consider the following program outline in which BB, CC, and QQ represent some code. begin integer x := 5; B: begin integer b1 := 6; BB end B end C: begin integer c1 := 7; CC end C proc Q() begin QQ end Consider forming independently specific programs as indicated in the table below. Unless otherwise stated, assume BB, CC, and QQ are empty. For each such program, the program s outcome is one of the following: CTE compile-time error : the program causes an error when compiled. RTE run-time error : the program compiles without error, but causes an error when run. NORMAL normal : compiles and runs without errors. In answering, focus on the central question: Can the code access a given variable? No tricks are intended. For example, assume that: the syntax is correct; empty code causes no error; and the language allows an invocation of Q to appear before Q s declaration. For each specific program and each kind of scoping indicated below, circle the correct outcome. static scoping dynamic scoping specific program outcome outcome CC is write(x); CTE RTE NORMAL CTE RTE NORMAL CC is write(b1); CTE RTE NORMAL CTE RTE NORMAL QQ is write(b1); and BB is Q(); CTE RTE NORMAL CTE RTE NORMAL QQ is write(b1); and CC is Q(); CTE RTE NORMAL CTE RTE NORMAL (b) (4 points) As seen in class and on the HW regarding the run-time implementation of static scoping, static pointers or the display are used to locate the proper stackframe for a variable in a lexically enclosing block. (Recall block Z is lexically enclosed by block Y if, in the program code, Z is nested within Y.) (circle one per line) This locating activity occurs within the symbol table at compile time. Once the proper stackframe is located, it is searched to find the variable. 2
(c) (13 points) The code below isidentical to that on the parameter passing question on the HW, except there is only one invocation of swap and that invocation uses actual parameters different from those on the HW. begin integer k; integer array a[1:2] proc swap(x, y, z); integer x, y, z; begin write (x, y, z); z:=x;x:=y;y:=z; end end k:=1;a[1] := 2; a[2] := 3; swap(k, a[k], k); write (k, a[1], a[2]); The above program has no "subscript out of range" or other errors. On the lines below, show the output from executing the above program with xbyname, y by value, and z by reference The point value of each answer is indicated below each line. (2 points) (1 point) (1 point) (3 points) (3 points) (3 points) End of Question 1. ( /25points ) 3
<<continue your answers here if necessary>> 4
Question 2. (25points ) Consider this Java code: interface I { abstract class A { class B extends A implements I { public int foo() {return 9; class C extends B { class D { (13 points) For each assignment below, place a check ( ) under the correct choice of legality determined. assignment i=a; i=b; i=c; a=i; a=(a) i; legality determined statically statically dynamically legal illegal (legal orillegal) and the declarations Aa;Bb;Cc;Dd;Ii; Assume each of these variables has been initialized. (8 points) For each method invocation below, place a check ( ) under the correct choice of legality determined. (Here, we re interested below inonly the static checking.) method invocation int n = a.foo(); int n = b.foo(); int n = c.foo(); int n = i.foo(); legality determined statically statically legal illegal d=(d) i; i=new D(); i=new I(); a=new I(); b=new I(); a=new A(); a=new B(); d=(d) new C(); (4 points) Indicate whether each of the following statements is correct. statement Class A must be declared as abstract because it does not define a concrete foo() Class C is abstract because it does not define a concrete foo() correct? (circle one per line) End of Question 2. ( /25points ) 5
Question 3. (25points ) (a) (7 points) Recall from lecture the relationships: is-a, e.g., a Leopard is a ZooAnimal. has-a, e.g., a Bear has a Color or a Bear has a reference to its name. Based on the relationships among classes on the HW, indicate whether each statement is correct. correct? statement (circle one per line) AJumble is-a Seq. AJumble has-a Seq. AJumble is-a JumbleIt. AJumbleIt is-a Jumble. AJumble has-a JumbleIt. AJumbleIt has-a Jumble. AJumbleIt has-a Seq. 6
(b) (12 points) Assume A standard implementation of C++ that uses the VMT approach for implementing dynamic binding (also known as dynamic dispatch), as discussed in lecture. Class C defines a non-virtual method s and a virtual method t. Cc; C* r; // r might point at a C object or an object of a class derived from C For each invocation, circle exactly one choice to indicate whether or not the implementation uses the VMT for the invocation. invocation uses VMT? (circle one per line) c.s() c.t() r s() r t() (*r).s() (*r).t() (c) (6 points) In a language like C++, the implementation of dynamic binding (dispatch) requires the symbol table to be searched at run-time. whether a particular method is a non-virtual method or a virtual method can be determined statically. whether a particular method invocation is a static binding or a dynamic binding can be determined statically. correct? (circle one per line) End of Question 3. ( /25points ) 7
Question 4. (25points ) For each Java program on the next page, indicate if it causes a compile-time error by circling compile-time error EXOR (exclusive or) show the program s output by filling in the provided lines (sufficient lines are provided). Hint: exactly one causes a compile-time error. The programs use: class MyExcpt1 extends Exception { public MyExcpt1(String msg){ super(msg); class MyExcpt2 extends Exception { public MyExcpt2(String msg){ super(msg); 8
public class Main { public static void main(string [] a) { try { d(); catch (MyExcpt1 e) { System.out.println("=M1 caught"); catch (MyExcpt2 e) { System.out.println("=M2 caught"); private static void d() throw new MyExcpt2("from d");//**2** (i) compile-time error EXOR show output: public class Main { public static void main(string [] a) { try { d(); catch (MyExcpt1 e) { System.out.println("=M1 caught"); catch (MyExcpt2 e) { System.out.println("=M2 caught"); throw new MyExcpt1("from catch 2"); private static void d() throw new MyExcpt2("from d");//**2** (ii) compile-time error EXOR show output: public class Main { public static void main(string [] a) try { d(); catch (MyExcpt1 e) { System.out.println("=M1 caught"); throw new MyExcpt2("from catch 2"); catch (MyExcpt2 e) { System.out.println("=M2 caught"); private static void d() throw new MyExcpt2("from d");//**2** (iii) compile-time error EXOR show output: public class Main { public static void main(string [] a) try { d(); catch (MyExcpt1 e) { System.out.println("=M1 caught"); catch (MyExcpt2 e) { System.out.println("+M2 caught"); try { throw new MyExcpt1("from catch 2"); catch (MyExcpt1 f) { System.out.println("=NESTED caught"); System.out.println("-M2 caught"); private static void d() throw new MyExcpt2("from d");//**2** (iv) compile-time error EXOR show output: End of Question 4. ( /25points ) 9
<<continue your answers here if necessary>> 10