



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Object-Oriented Programming and Data Structures; Subject: Computer Science; University: Cornell University; Term: Spring 2007;
Typology: Study notes
1 / 7
This page cannot be seen from the preview
Don't miss anything!




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 /15 /24 /10 /15 /21 /15 / Grader
There are 2 base cases, k = 0 and k = 1. For k = 0, the only tree of height 0 is a leaf, which has 1 node, and 1 > 0 = f 0. For k = 1, the smallest nearly balanced tree of height 1 is a node with a single child, which has 2 nodes, and 2 > 1 = f 1. That’s it for the basis.
Now let T be a nearly balanced tree of height k ≥ 2. The strong induc- tion hypothesis states that the theorem holds for all nearly balanced trees of height strictly less than k. At least one of T ’s subtrees (call it A) must have height exactly k − 1. The other subtree (call it B) must exist, otherwise condition (ii) in the definition of nearly balanced would fail, and B must have height at least k − 2 by condition (iii). By the strong induction hypothesis, A must have at least fk− 1 nodes and B must have at least fk− 2 nodes, thus T has at least fk− 1 + fk− 2 + 1 > fk nodes.
(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 { static int aCount = 0; static int bCount = 0; void m() { if (this instanceof B) bCount++; else aCount++; } } 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); } } Make the constructor with 3 arguments check that both left and right are null or both are nonnull. Make the left and right fields private instead of public and access them with public getters and setters. Have just one setter that sets both left and right simultaneously, checking that both arguments are null or both are nonnull.
(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 meth- ods 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) { if (c1 == null && c2 == null) return 0; if (c1 == null) return -1; if (c2 == null) return 1; if (c1.datum < c2.datum) return -1; if (c1.datum > c2.datum) return 1; return lex(c1.next, c2.next); }
End of Exam