






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 2005;
Typology: Study notes
1 / 10
This page cannot be seen from the preview
Don't miss anything!







Write your name and Cornell netid above. There are 6 questions on 9 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
∑^ n−^1
i=
i(i + 1) =
n^3 − n 3
Label clearly the parts of your proof. In the induction step, state the induction hypothesis and indicate exactly where in your argument it is used. Make sure your argument is perfectly clear—any ambiguity whatsoever will result in loss of points.
(c) class Fs { static String s = "hello"; public static void main(String[] args) { new Fs().foo(); System.out.println(s); } void foo() { String s = "world"; } }
(d) class Wg { static int x = 0; int y; Wg() { y = x++; } public String toString() { return String.valueOf(y); } public static void main(String[] args) { String s = new Wg().toString(); s += new Wg().toString(); s += new Wg().toString(); System.out.println(s); } }
(e) class Rts { public static void main(String[] args) { System.out.println(zorg(new RtsC())); } static int zorg(RtsA x) { return x.f()*10 + x.a; } } abstract class RtsA { int a = 2; int f() { return a; } } class RtsB extends RtsA { int a = 3; int f() { return a; } } class RtsC extends RtsB { int a = 4; }
(f) class Yfk { public static void main(String[] args) { System.out.println(new YfkC().x); } } abstract class YfkA { int x = 3; YfkA() { x++; } } class YfkB extends YfkA {} class YfkC extends YfkB {}
(a) Explain, in at most 50 words, the difference between reference types and primitive types in Java.
(b) Explain, in at most 50 words, the difference between static types and dynamic types in Java.
class TreeCell { Object datum; TreeCell left, right; ... }
class ListCell { Object datum; ListCell next; ListCell(Object obj, ListCell c) { datum = obj; next = c; } ListCell(Object obj) { this(obj, null); } }
Write a recursive method tree2List that, for a given binary tree, creates a list of the elements in preorder. For each TreeCell in the input tree, there should be exactly one ListCell with the same datum in the output list, and the order of elements in the list from front to back should be the same as preorder in the original tree. (Hint. Create the list from back to front. Use the second argument of the helper function to pass the accumulated list so far.)
static ListCell tree2List(TreeCell t) {return tree2List(t,null);} static ListCell tree2List(TreeCell t, ListCell c) {
char charAt(int index) returns the char value at the specified index int length() returns the length of this string
Indexing starts at 0. Your method should return a value even if the argument is not a String.
public boolean equals(Object obj) {
End of Exam