



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




(e.g., dck10, not your Cornell id)
Write your name and Cornell netid above. There are 6 questions on 6 numbered pages. Check now that you have all the pages. Write your answers in the boxes provided. Use the back of the pages for workspace. Ambiguous answers will be considered incorrect. The exam is closed book and closed notes. Do not begin until instructed. You have 90 minutes. Good luck!
Score /16 /16 /16 /16 /16 /20 / Grader
(a) (8 points) Suppose you had a method m() of a class A inherited by a subclass B. Say you would like to know how many times during the run of the program m() is called as a member of an object of dynamic type B and how many times it is called as a member of an object of dynamic type A. Is it possible to modify the method m() and the class A to keep track of this information? If so, show how. If not, say why not. class A {
void m() {
} }
class B extends A {}
(b) (8 points) A binary tree is full if every node has either 0 or 2 children. Suppose you had the following definition of a TreeNode class, and wanted to modify it to enforce fullness. You would like to make sure that no tree can ever be constructed that is not full (including later modification), while still allowing the left and right children of the node to be read and changed. Say briefly (in words) how the class could be modified to accomplish this. class TreeNode { public Object datum; public TreeNode left, right;
public TreeNode(Object datum, TreeNode left, TreeNode right) { this.left = left; this.right = right; this.datum = datum; }
public TreeNode(Object datum) { this(datum, null, null); } }
(a) T F A successful cast (Integer)x changes the dynamic type of the object occupying x to Integer. (b) T F The cast (Integer)x changes the static type of the variable x to Integer. (c) T F A call to super or this in a constructor must occur first. (d) T F A static field is shared by all objects of the class. (e) T F A non-abstract subclass of an abstract class A must implement all methods declared abstract in A. (f) T F Abstract classes and interfaces cannot be instantiated. (g) T F int[] is a reference type. (h) T F Local variables of a method can be declared static provided the method is declared static. (i) T F Shadowing and overriding are the same thing. (j) T F Any inductive proof using weak induction can also be done using strong induction. (k) T F The main method must be static. (l) T F A method in an abstract class must have an empty body. (m) T F Uncaught RuntimeExceptions must be declared in the method header. Questions (n)–(p) refer to the following grammar for numbers in scientific notation. Spaces are not significant.
(n) T F The following is a sentence: 1.0618E- (o) T F The following is a sentence: 9. (p) T F The number of possible sentences is infinite.
ListCell { int datum; ListCell next; }
We would like to compare two lists lexicographically, that is, in dictionary order. The definition of lexicographic order is as follows.
(a) Find the first cell position where the data in two lists differ, if such a position exists. The list with the smaller datum in that position is lexicographically smaller. (If we were talking about words in the dictionary, this would be like “aardvark” < “abacus”.) (b) If no such position exists and one list is longer than the other, that is, if the shorter list is a prefix of the longer, then the shorter is lexicographically smaller. (Like “cat” < “catnip”.) (c) Otherwise the lists are equal.
Write a recursive method lex that compares two lists lexicographically. It should take two lists and return a negative number if the first is lexicographically less than the second, 0 if they are equal, and a positive number if the second is lexico- graphically less than the first. public static int lex(ListCell c1, ListCell c2) {
End of Exam