Docsity

CS211 Fall 2005 Exam: Problem Solving and Data Structures - Prof. K. Birman, Exams of Computer Science

Toggle sidebar
bg1
Name____________________________________________
Page 1 of 9
NetID______________
Prelim One
CS211 — Fall 2005
Closed book; closed notes; no calculators. Write your name and netID above. Write your name
clearly on each page of this exam. For partial credit, you must show your work.
Do not begin until you are instructed to do so. You have 90 minutes.
Problem Max
Points
Your
Score
Grader
120
220
310
424
521
65
Total 100
Bonus
Points
Max
Points
Your
Score
Grader
B1 5
B2 10
B3 5
Total 20
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download CS211 Fall 2005 Exam: Problem Solving and Data Structures - Prof. K. Birman and more Exams Computer Science in PDF only on Docsity!

NetID______________

Prelim One

CS211 — Fall 2005

Closed book; closed notes; no calculators. Write your name and netID above. Write your name clearly on each page of this exam. For partial credit, you must show your work.

Do not begin until you are instructed to do so. You have 90 minutes.

Problem Max

Points

Your

Score

Grader

Total 100

Bonus

Points

Max

Points

Your

Score

Grader

B1 5

B2 10

B3 5

Total 20

  1. ( 20 points; 2 each ) True or false?

a) T F The statement “String s;” calls the constructor of class Object.

b) T F The protected modifier signifies that the method or variable can only be accessed from within its class, from within its subclasses, or from within classes in the same package.

c) T F In Java, a class may directly inherit from more than one abstract class.

d) T F If class A inherits from abstract class B, and B implements an interface C, then a cast from an object of type A to an object of type C will always succeed. Example: A a; ... C c = (C) a;

e) T F In Java, an interface can extend multiple interfaces.

f) T F Strong induction is much better than weak induction, and therefore we should use it whenever possible.

g) T F In Java, objects are stored in the stack frames.

h) T F If a variable is declared to hold a primitive type then its static and dynamic types are always the same.

i) T F During a Java program’s execution, a single method can correspond to multiple stack frames.

j) T F Any binary tree of depth 2 has ≤ 7 nodes.

  1. ( Recursion; 10 points ) Write a recursive method (called removeEven) to remove the elements at even positions in a linked list. Assume that your method takes a single argument of type ListCell and that it returns a value of type ListCell; the declaration for ListCell is given below.

class ListCell { public Object datum; // Data for this cell public ListCell next; // Next cell }

  1. ( Types; 24 points; 3 each ) Consider the following interface and class declarations.

interface I1 {...} interface I2 {...} interface I3 extends I1 {...} class C1 implements I1 {...} class C2 implements I2 {...} class C3 implements I3 {...} class C4 extends C3 implements I2 {...}

For each of the following snippets of Java code, determine whether the code will produce no error, a run-time error, or a compile-time error. (Assume that each snippet is tested independently of the others.)

a) I2 a = new I2();

b) I2 b = new C2();

c) C3 c = new C4();

d) C2 d = new C4();

e) C4 e = new C3();

f) C4 f = (C4)(new C3());

g) I1 g1 = new C1(); C4 g2 = new C4(); g1 = g2;

h) I1 g1 = new C4(); I2 g2 = new C2(); g2 = g1;

  1. ( Recursion; 5 points ) Write Java code for a class MainProblem that contains just a single method: the main method. When run using the command line with one argument, MainProblem should print the argument followed by the argument without its first character, followed by the argument without its next character, etc. Example:

java MainProblem hello

Output: hello ello llo lo o

Specifications:

  • Assume legal input.
  • Do not use any loop control-structures (i.e., no for-loops, no while-loops, etc.).
  • There should be no methods other than the main method.
  • Do not use any class variables.

Hint: You are likely to find the substring method of class String to be useful. Here is its documentation from the Java API.

String substring(int beginIndex) Returns a new string that is a substring of this string. String substring(int beginIndex, int endIndex) Returns a new string that is a substring of this string.

Bonus Point Questions

Work on these only if you have extra time. Bonus points are not used in determining your final course grade unless you are on a grading borderline.

B1. What is the answer to this question?

B2. Consider the following two classes. What is printed when the main method is executed? (You will probably need to mark off lengths on a straightedge to distinguish between variables. You can tear off a scrap of paper from the last page for this purpose, but we still want your course feedback and the feedback is worth 5 bonus points.)

public class __________ { static int _________; public static void main(String[] ___________) { ________ ________ = new ________( (new ________(++__________._________)).________ ); ________(________.________(________.________)); ________(________.________(________).________); ________(________(________.________(________))); } // method main static int ________(________ ________) {return ________.________;} public static void ________(int ________) {System.out.println(________);} } // class __________

class ________ { int ________; ________(int ________) { this(________,________++); this.________=________; } ________(int ________, int _________) { this.________+=_________+=this.________+________++; } int ________(int ________) { ________(this); return ________; } ________ ________(________ ________) { ________.________+=++__________._________; return new ________(________.________); } } // class ________