6 Solved Problems on Java Programs of Functions | CS 2110, Study notes of Computer Science

Material Type: Notes; Class: Object-Oriented Programming and Data Structures; Subject: Computer Science; University: Cornell University; Term: Spring 2005;

Typology: Study notes

Pre 2010

Uploaded on 08/30/2009

koofers-user-zml
koofers-user-zml 🇺🇸

10 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS211 Spring 2005
Prelim 1
March 10, 2005
Name NetId
Instructions
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!
123456Σ
Score /15 /24 /10 /15 /21 /15 /100
Grader
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download 6 Solved Problems on Java Programs of Functions | CS 2110 and more Study notes Computer Science in PDF only on Docsity!

CS211 Spring 2005

Prelim 1

March 10, 2005

Name NetId

Instructions

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

  1. (15 points) Prove by induction that for all n ≥ 0,

∑^ 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 {}

  1. (10 points)

(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.

  1. (15 points) Binary trees and lists were defined in class in terms of TreeCell and ListCell. Lists are terminated by null, and one or both children of a TreeCell may be null.

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) {

  1. (15 points) Write an equals method for String. You may use the following methods from the String class:

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