Docsity

Exam 1 Solutions for Computer Programming and Problem Solving | CS 111, Exams of Computer Science

Toggle sidebar
bg1
CS111 EXAM 1
Spring Semester 2002
Thursday, February 28/Friday, March 1
YOUR NAME: Exam 1 Solutions
This exam has five problems. Each problem has several parts. The number of points for each
problem is shown in square brackets next to the problem. There are 100 total points on the exam.
Write all your answers on the exam itself. Whenever possible, show your work so that partial
credit can be awarded.
The exam is open book. You may refer to your notes, handouts from this semester (including
the Fall’01 CS111 text), problem sets and solutions, and whatever additional materials would be
helpful. However, you may not use another person’s notes, or any materials from
previous semesters (excluding the Fall’01 CS111 text).
By the Honor Code, you are not allowed to talk to anyone about the details of this
exam before or after taking it, until after all members of the class have taken the
exam.
Please keep in mind the following tips:
•Briefly skim the entire exam before starting any problem.
•Work first on the problems on which you feel most confident. You do not have to do the
problems in order. All problems are independent from each other.
•Try to do something on every problem so that you can potentially receive partial credit. A
guess is better than no answer at all!
•Allocate your time carefully. If you are taking too long on a problem, wrap it up and move
on.
•If you finish early, go back and check your answers.
GOOD SKILL!
Problem Topic Points Score
1 Java Syntax 15
2 BuggleWorld Execution 20
3 Writing Methods 25
4 Invocation Trees 20
5 Debugging 20
Total 100
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Exam 1 Solutions for Computer Programming and Problem Solving | CS 111 and more Exams Computer Science in PDF only on Docsity!

CS111 EXAM 1

Spring Semester 2002 Thursday, February 28/Friday, March 1

YOUR NAME: Exam 1 Solutions

This exam has five problems. Each problem has several parts. The number of points for each problem is shown in square brackets next to the problem. There are 100 total points on the exam.

Write all your answers on the exam itself. Whenever possible, show your work so that partial credit can be awarded.

The exam is open book. You may refer to your notes, handouts from this semester (including the Fall’01 CS111 text), problem sets and solutions, and whatever additional materials would be helpful. However, you may not use another person’s notes, or any materials from previous semesters (excluding the Fall’01 CS111 text).

By the Honor Code, you are not allowed to talk to anyone about the details of this exam before or after taking it, until after all members of the class have taken the exam.

Please keep in mind the following tips:

  • Briefly skim the entire exam before starting any problem.
  • Work first on the problems on which you feel most confident. You do not have to do the problems in order. All problems are independent from each other.
  • Try to do something on every problem so that you can potentially receive partial credit. A guess is better than no answer at all!
  • Allocate your time carefully. If you are taking too long on a problem, wrap it up and move on.
  • If you finish early, go back and check your answers.

GOOD SKILL!

Problem Topic Points Score

1 Java Syntax 15

2 BuggleWorld Execution 20

3 Writing Methods 25

4 Invocation Trees 20

5 Debugging 20

Total 100

This page intentially left blank (except for this self-referential sentence).

Problem 2 [20]: Buggle World Execution Consider the two Java classes in Fig. 1.

public class DoItWorld extends BuggleWorld {

public void run () { DoItBuggle dewey = new DoItBuggle(); // run statement 1 int n = 5; // run statement 2 dewey.setPosition(new Point(n,n-2)); // run statement 3 * dewey.brushUp(); // run statement 4 dewey.doit(Color.green, n-1); // run statement 5 * dewey.doit(Color.blue, n+1); // run statement 6 * dewey.forward(); // run statement 7 dewey.brushDown(); // run statement 8 dewey.forward(3); // run statement 9 * } }

public class DoItBuggle extends Buggle {

public void doit (Color c, int n) { Color oldColor = this.getColor(); this.setColor(c); this.forward(n); this.brushDown(); this.backward(n-2); this.brushUp(); this.backward(2); this.left(); this.setColor(oldColor); }

}

Figure 1: Two Java classes.

Suppose that the run() method is invoked on an instance of DoItWorld which has a 10 × 10 grid of cells. In the four grids on the following page, show the state of the grid directly after the execution of each of the statements in the run() method body marked with a *. In each grid, you should show the following:

  1. Draw buggle dewey as a triangle “pointing” in the direction that the buggle is facing.
  2. Indicate the current color of the buggle by putting the first letter of the color name inside the triangle (e.g. B for blue, G for green, etc.).
  3. Indicate the color of each non-white grid cell by putting the first letter of the color name inside the cell (e.g. B for blue, G for green, etc.).

DoItWorld grid after the DoItWorld grid after the execution of run() statement 3 execution of run() statement 5

0 1 2 3 4 5 6 7 8 9 10

0

1

2

3

4

5

6

7

8

9

10

R

0 1 2 3 4 5 6 7 8 9 10

0

1

2

3

4

5

6

7

8

9

10

R G^ G

DoItWorld grid after the DoItWorld grid after the execution of run() statement 6 execution of run() statement 9

0 1 2 3 4 5 6 7 8 9 10

0

1

2

3

4

5

6

7

8

9

10

G G

B

B

B

B

R

0 1 2 3 4 5 6 7 8 9 10

0

1

2

3

4

5

6

7

8

9

10

G G

B

B

B

B

R R R R

Put your definition of the e method here.

Here is one solution, which does not name any intermediate pictures:

// The letter “E” can be formed by overlaying an “F”

// and a copy of “F” flipped across the horizontal axis.

public void Picture e (Color c) {

return overlay(f(c), flipHorizontally(f(c)));

Here is another solution, which names some intermediate pictures:

public void Picture e (Color c) {

Public picture f c = f(c);

Public picture f c flipH = flipHorizontally(f c);

return overlay(f c, f c flipH);

A surprising number of students tried to draw the “E” shape with buggles. As we have seen on problem sets, buggles can indeed draw letters in BuggleWorld, but there is no way for a buggle to somehow turn what it has drawn into a Picture instance within PictureWorld.

Put your definition of the fame method here.

Here is one solution, which does not name any intermediate pictures:

// Create a picture consisting of quadrants “F”,“A”, ‘M”,“E”

// where “F” and “E” are in color c1 and “A” and ‘M” are in color c

public void Picture fame (Color c1, Color c2) {

return fourPics(f(c1), // F

overlay(f(c2),

flipVertically(f(c2))), // make A out of 2 Fs

clockwise90(e(c2)), // M is rotated E

e(c1)); // E

Here is another solution, which names some intermediate pictures:

public void Picture fame (Color c1, Color c2) {

Picture f c1 = f(c1);

Picture f c2 = f(c2);

Picture a c2 = overlay(f c2, flipVertically(f c2));

Picture m c2 = clockwise90(e(c2));

Picture e c1 = e(c1);

return fourPics(f c1, a c2, m c2, e c1); }

Problem 4 [20]: Invocation Trees

public class ExamPictureWorld extends PictureWorld {

public Picture meth1 (Picture a) { Picture b = beside(a, empty()); return overlay(meth2(b), b); }

public Picture meth2 (Picture c) { return clockwise90(above(c, empty(), 0.75)); } }

Figure 2: A sublcass of PictureWorld.

Consider the subclass of PictureWorld shown in Fig. 2. Supose that: EPW is an instance

of ExamPictureWorld, P0 is a Picture instance denoting the empty picture, P1 is a Picture instance denoting the rightmost picture below:

ExamPictureWorld EPW

Picture P0 Picture P

red blue

The dashed grid lines are not part of the pictures. They indicate coordinates within pictures. The colors names are not part of picture P1. They indicate the color of the two rectangles. Each of the two rectangles is a solid color without any separately colored border. On the next page, you are to draw an invocation tree that models the instance method invocation EPW .meth1( P1 ). In the area labeled Execution Land, you should draw an invocation tree that contains the following eight nodes, arranged appropriately into a tree. You should use each node exactly once.

above( , , ): clockwise90( ): empty(): meth2( ):

beside( , ): empty(): meth1( ): overlay( , ):

The empty circles in the nodes are skeletons for object references that you should fill in with one of the labels P0, P1, P2, P3, P4, or P5 to refer to the appropriate Picture instance in Object Land (see below). A circle enclosed by parentheses is a reference to an actual argument of the method invocation. A circle appearing after a colon is a reference to the result of the method invocation. The root of the invocation tree is the meth1() node, which has already been drawn for you, and whose actual argument has been filled in (you need to fill in its result). In the area labeled Object Land are the skeletons for the six Picture instances that are used during the execution. The pictures labeled P0 and P1 have already been drawn for you; you

should draw the pictures for P2 , P3 , P4 , and P5. In each picture, you should label red areas with the letter R and blue areas with the letter B. All other areas are presumed to be white. (Note: for simplicity, the receiver object EPW for each of the method invocations has been omitted. This instance has also been omitted from Object Land.)

Problem 5 [20]: Debugging

Figure 3: In the table on the next page, for each of 10 errors in different lines of the above program give:

  1. the line number of the error,
  2. a brief description of the error, and
  3. a corrected version of the line (i.e., with the error fixed).

You may list the errors in any order. You do not have to list them in the order in which they occur

  • public class ExamBuggleWorld extends BuggleWorld { \ line The class declarations in Fig. 3 contain (at least) 10 errors (syntax errors and type errors). - \ line
    • public void run () { \ line - Color c = Color.cyan(); \ line - int n = 4 \ line - ExamBuggle emma = ExamBuggle(); \ line - emma.mystery1(c,n); \ line - emma.mystery1(3,Color.red); \ line - boolean answer = emma.mystery2(); \ line - this.mystery3(); \ line
    • } \ line
  • } \ line - \ line
  • class ExamBuggle extends Buggle { \ line - \ line
    • public void mystery1(Color c, int n1) { \ line
      • n2 = n1 + 1; \ line
      • this.setColor(Color.c); \ line
      • forward(n2); \ line
      • this.dropBagel(); \ line - \ line
    • public boolean mystery2() { \ line
      • this.isOverBagel(); \ line
    • } \ line - \ line
    • public mystery3() { \ line
      • this.dropBagel(); \ line
    • } \ line - \ line
  • } \ line

Error # Line # Brief description of error (^) Corrected line

1 4 Color.cyan()^ is not a method invocation

Color.cyan

The local variable declaration int n = 4 is missing a semi-colon at the end

int n = 4;

There is a missing new in the constructor method invocation that creates an ExamBuggle

ExamBuggle emma = new ExamBuggle()

The two arguments of the instance method invocation emma.mystery1(3,Color.red) are in the wrong order

emma.mystery1(Color.red,3)

In this.mystery3(), this stands for an instance of ExamBuggleWorld, which does not understand the mystery3 message; the recipient should be an instance of ExamBuggle

emma.mystery3;

The local variable declaration n2 = n1 + 1; is missing a type for the contents of the variable

int n2 = n1 + 1;

Color.c attempts to reference a non-existent class constant rather than the parameter c

this.setColor(c);

The instance method declaration for mystery1 is missing a close squiggly brace.

The non-void method mystery2 is missing a return statement.

return this.isOverBagel();

The method header for mystery3 is missing the return type, void

public void mystery3()

Notes:

  • Another (unintended) error was that the wrong symbol (\) was used to introduce comments in the problem. The correct symbol is //.
  • Some students said there was a missing public class ExamBuggle extends Buggle on line
    1. It turns out that this is not an error, but we did not take off for it because we haven’t explained this in class. It turns out that a single Java file can declare only one public class. If the two class declarations in Fig. 3 were in two separate files, each could be declared public.

THIS IS THE END OF THE EXAM.