






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!







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. Basis: For n = 0, the left-hand side is the empty sum, which by convention is 0, and the right-hand side evaluates to 0.
Induction step: Let n ≥ 0. Assume that
∑^ n−^1
i=
i(i + 1) =
n^3 − n 3
This is the induction hypothesis. We wish to prove under this assumption that ∑^ n
i=
i(i + 1) =
(n + 1)^3 − (n + 1) 3
Starting from the left-hand side,
∑^ n
i=
i(i + 1) =
∑^ n−^1
i=
i(i + 1) + n(n + 1) by taking out the last term
n^3 − n 3
(n + 1)^3 − (n + 1) 3
by elementary algebra.
(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"; } } hello
(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); } } 012
(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; } 32
(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 {} 4
(a) Explain, in at most 50 words, the difference between reference types and primitive types in Java.
Reference types are class names, interface names, and array types. A variable of reference type does not contain the data itself, but rather a pointer to a heap-allocated object. Primitive types are types such as int, boolean, and char. Variables of primitive type contain the data itself.
(b) Explain, in at most 50 words, the difference between static types and dynamic types in Java.
Static types are types of expressions in the program. They are determined at compile time from declarations. Dynamic types are the type of objects created at runtime, usually by a new instruction.
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) { if (t == null) return c; c = tree2List(t.right, c); c = tree2List(t.left, c); return new ListCell(t.datum, 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) { if (!(obj instanceof String)) return false; String s = (String)obj; if (length() != s.length()) return false; for (int i = 0; i < length(); i++) { if (charAt(i) != s.charAt(i)) return false; } return true; }
End of Exam