Docsity

CS211 Spring 2007 Prelim 1, Study notes of Computer Science

Toggle sidebar
bg1
CS211 Spring 2007
Prelim 1
March 8, 2007
Name NetId(e.g., dck10, not your Cornell id)
Instructions
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!
123456Σ
Score /16 /16 /16 /16 /16 /20 /100
Grader
pf3
pf4
pf5

Partial preview of the text

Download CS211 Spring 2007 Prelim 1 and more Study notes Computer Science in PDF only on Docsity!

CS211 Spring 2007

Prelim 1

March 8, 2007

Name NetId

(e.g., dck10, not your Cornell id)

Instructions

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

  1. (16 points) A node of a binary tree is nearly balanced if either (i) the node is a leaf, (ii) the node has one child and that child is a leaf, or (iii) the node has two children and the heights of its left and right subtrees differ by no more than 1. A binary tree is nearly balanced if all its nodes are nearly balanced. Prove by induction that the number of nodes in a nearly balanced binary tree of height k is at least fk, the kth Fibonacci number. Recall f 0 = 0, f 1 = 1, and fk = fk− 1 + fk− 2 for k ≥ 2. Label clearly the parts of your proof. In the induction step, state the induction hypothesis clearly and indicate exactly where in your argument it is used. Hint. You will probably need strong induction, and there will probably be two base cases.
  1. (16 points)

(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); } }

  1. (16 points) True or false?

(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. is the start symbol of the grammar.

→ 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 → 0 | → | → E+ | E− → . | −. | 0. 0

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

  1. (20 points) Say we are given null-terminated integer lists built from ListCells

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