Docsity

CS100J Prelim I Exam - Prof. D. Gries, Exams of Computer Science

Toggle sidebar
bg1
1
NAME (PRINT LEGIBLY!)___________________________________________
(last, first)
This 90-minute exam has 6 questions worth a total of 100 points. Spend a few minutes looking at all
questions before beginning so that you can see what is expected. Budget your time wisely. Use the
back of the pages, if you need more space.
Question 0 (2 points). Write your netid and your name, legibly, at the top of each page.
Question 1 (15 points). Write the body of method fixZip,
specified below. The following methods of class String may be
useful:
• s.length() = the number of characters in s.
• s.charAt(k) = the character at position (or index) k of string s
• s.substring(h, k) = a string consisting of characters s[h], s[h+1], …, s[k–1]
• s.substring(h) = a string consisting of characters s[h], s[h+1], …, s[s.length()–1]
• s.indexOf(c) = index of first occurrence of character c in s (-1 if none)
• s.indexOf(s1) = index of first char of the first occurrence of String s1 in s (-1 if none)
/** s, an address, may be a Cornell address with the wrong zip number. If s contains the substring
"Cornell" and also the substring "14850", then return s but with "14850" replaced by "14853".
Otherwise, return s.
E.g. for s = "Gries; Olin 167; Cornell; Ithaca, NY 14850; USA",
return "Gries; Olin 167; Cornell; Ithaca, NY 14853; USA". */
public String fixZip(String s) {
}
CS100J Prelim I, 25 Sept. 2007 CORNELL NETID ______________________
pf3
pf4
pf5

Partial preview of the text

Download CS100J Prelim I Exam - Prof. D. Gries and more Exams Computer Science in PDF only on Docsity!

NAME (PRINT LEGIBLY!)___________________________________________

(last, first)

This 90-minute exam has 6 questions worth a total of 100 points. Spend a few minutes looking at all

questions before beginning so that you can see what is expected. Budget your time wisely. Use the

back of the pages, if you need more space.

Question 0 (2 points). Write your netid and your name, legibly, at the top of each page.

Question 1 (15 points). Write the body of method fixZip,

specified below. The following methods of class String may be

useful:

  • s.length() = the number of characters in s.
  • s.charAt(k) = the character at position (or index) k of string s
  • s.substring(h, k) = a string consisting of characters s[h], s[h+1], …, s[k–1]
  • s.substring(h) = a string consisting of characters s[h], s[h+1], …, s[s.length()–1]
  • s.indexOf(c) = index of first occurrence of character c in s (-1 if none)
  • s.indexOf(s1) = index of first char of the first occurrence of String s1 in s (-1 if none)

/** s, an address, may be a Cornell address with the wrong zip number. If s contains the substring

"Cornell" and also the substring "14850", then return s but with "14850" replaced by "14853".

Otherwise, return s.

E.g. for s = "Gries; Olin 167; Cornell; Ithaca, NY 14850; USA",

return "Gries; Olin 167; Cornell; Ithaca, NY 14853; USA". */

public String fixZip(String s) {

CS100J Prelim I, 25 Sept. 2007 CORNELL NETID ______________________

Question 2 (20 points). Below are two class definitions, for classes P and S.

(a) Draw a manilla folder (instance, object) of class P.

(b) Draw a manilla folder of class S. Note that both fields are given by initializing declarations. You

have to draw the values in these fields correctly. If this requires drawing new folders, do so.

public class S extends P {

public static double pi= 3.14;

private P one= new P();

private P two = null;

public class P {

public static int x= 3;

private S s= null ;

public void m ( String p) {

x= p.length();

public void setX( int v) {

x= v;

x= x + 1;

Question 4 (20 points). Below is a class Point as well as three variables. Execute the following

statements, one after the other, and show what is printed by each println statement in the space

provided afer it. Hint : You won't be able to do this properly unless you draw all the objects that are

created and assign properly to the variables. We suggest using the back of the previous page for this.

/** A point (x, y) in the plane. */

public class Point {

private int x= 0;

private int y= 0;

/** Constructor: The point (0,0). */

public Point() {}

/** Constructor: The point (0,y). */

public Point( int y) {

this .y= y;

/** Constructor: the point (x,y). */

public Point( int x, int y) {

this .x= x; this .y= y;

/** = a representation of this Point. */

public String toString() {

return "(" + x + ", " + y + ")";

/** = the x-coordinate of this point. */

public int getX() {

return x;

/** = the y-coordinate of this point. */

public int getY() {

return y;

/** Set P's x-coordinate to

this object's x-coordinate + 3. */

public void setX(Point p) {

p.x= this .x + 3;

b null c null

d null

b= new Point(5);

c= new Point(6, 7);

d= b;

System.out.println("d " + d);

c.setX(b);

System.out.println("b " + b);

System.out.println("c " + c);

System.out.println("d " + d);

b= new Point();

System.out.println("b " + b);

System.out.println("d " + d);

Question 5 (20 points).

(a) Define the term “parameter”.

(b) Explain how a new-expression like new C(5, 3); is executed.

(c) Explain how to execute an assignment statement < variable >= < expression >;

(d) What is a local variable, and what is its scope?

(e) Suppose a function body is being executed because of a function call. What kind of statement

causes the function body to terminate?

0 ______________ out of 02 1 ______________ out of 20 2 ______________ out of 20 3 ______________ out of 18 4 ______________ out of 20 5 ______________ out of 20 Total ___________ out of 100