Pages

Exam9-APCSA-17-18

This exam consists of 5 multiple choice questions worth 5 points each, one short answer question worth 25 points, and one coding exercise worth 50 points (25 points for literacy and 25 points for technical accuracy). For each multiple choice question there is at least one correct answer, and there might be more than one correct answer.

Instructions

  1. Write your last name in the upper right hand corner of each page of this exam.
  2. Below your last name, write the name you prefer to be called in class in parentheses.
  3. For each multiple choice question, circle the letter of the correct answer(s).
  4. Follow the instructions for the coding exercise that are provided in that section of the exam.
Each multiple choice question will be graded as follows.
  • Five points will be awarded if and only if all of the correct answers and none of the incorrect answers are circled.
  • Two points will be deducted for each correct answer that is not circled.
  • Three points will be deducted for each incorrect answer that is circled.
  • No more than five points will be deducted per question.

The following grading scale, which is analogous to the scale used for grading programming assignments, will be used when scoring the short answer question and the two components of the coding exercise.

MarkPoints
++26
+25
✓+24
23
✓-21
-19
--17
00

Don’t Panic!

The road to wisdom? — Well, it's plain
and simple to express:
Err
and err
and err again
but less
and less
and less.
—Piet Hein

Q1 = questions[0]; // 5 points

The authors of Computer Science: An Interdisciplinary Approach formally define the idea of a data type as a set of X and a set of Y, where X and Y are what?

  1. values and operations
  2. ones and zeros
  3. names and values
  4. operations and operands
  5. methods and parameters
  6. none of the above
Correct:11
Incorrect:11

Q2 = questions[1]; // 5 points

Which of the following Java Boolean expressions evaluate to true?

  1. true || false
  2. true && false
  3. (true || false) && (!true || !false)
  4. (1 / 2 > 0)
  5. (1.0 / 2 > 0)
  6. (1.0 / 2.0 > 0)
Correct:17
Incorrect:5

Q3 = questions[2]; // 5 points

What does the following Java program print?

public class TF
{
    public static boolean print(boolean p)
    {
        System.out.print(p ? "T" : "F");
        return p;
    }
    public static boolean t() { return print(true); }
    public static boolean f() { return print(false); }
    public static void main(String args[])
    {
        print( (f() && t() && f()) || t() );
    }
}
  1. T
  2. F
  3. FT
  4. FTT
  5. FTF
  6. FTFTT
Correct:1
Incorrect:21

Q4 = questions[3]; // 5 points

Which of the following statements about Java constructor methods are true?

  1. Constructors may be invoked using either the keyword new, the keyword this, or the keyword super.
  2. The name of a constructor method must be the same as the name of the class it belongs to.
  3. The signature of a constructor method does not include a return type.
  4. The return type of a constructor method may be one of Java's eight primitive types.
  5. A class may have more than one constructor method.
  6. The constructor for class that extends another class always calls a constructor method of the superclass after all of the other statements in the constructor method have been executed.
Correct:15
Incorrect:7

Q5 = questions[4]; // 5 points

Which of the following statements are true?

  1. I’m glad this exam doesn’t have any DEK questions.
  2. I would watch DEK videos even if they weren’t assigned.
  3. DEK is boring.
  4. I wish I had my own copies of TAOCP. (I once had a dream about a signed first edition.)
  5. I didn’t know that one of Piet Hein’s grooks is engraved on the entryway to DEK’s house.
  6. This question won’t compile, because DEK and TAOCP aren’t defined.
Correct:1
Incorrect:21

Q6 = questions[5]; // 25 points

What does the following Java program print?

/**
 * A problem sometimes known as Moser’s circle problem is to
 * determine the number of regions into which a circle is divided
 * if points on its circumference are joined by chords and no three
 * chords intersect at a single point inside the circle.
 */
public class CircleDivisionByChords
{
    private static void print(int p, int r)       // P0
    {
        // number of chords
        final int c = (p * (p - 1)) / 2;          // P1

        System.out.print(p + " point");
        System.out.print(p == 1 ? ", " : "s, ");
        System.out.print(c + " chord");
        System.out.print(c == 1 ? ", " : "s, ");
        System.out.print(r + " region");
        System.out.print(r == 1 ? "\n" : "s\n");
    }
    public static void main(String[] args)
    {
        int p = 0; // number of points            // M1
        int r = 1; // number of regions           // M2
        print(p, r);                              // M3/P0
        print(++p, r);                            // M4/P0
        print(++p, ++r);                          // M5/P0
        int i=1, j=1, k=0;                        // M6
        while (k < 4) {
            j += k;                               // M7
            i += j;                               // M8
            r += i;                               // M9
            print(++p, r);                        // M10/P0
            k++;                                  // M11
        }
    }
}

Write your answer in the space provided on the next page.

Steppcrijk
M1
M2
M3/P0
P1
M4/P0
P1
M5/P0
P1
M6
M7
M8
M9
M10/P0
P1
M11
M7
M8
M9
M10/P0
P1
M11
M7
M8
M9
M10/P0
P1
M11
M7
M8
M9
M10/P0
P1
M11
Use this table to record the values of variables after the execution of each step.

Answer (program output)

Correct:20
Incorrect:2

Coding Exercise (50 points)

Write a Java program that satisfies the following requirements:

  1. Your program should include two interfaces.
    1. Your program should include an interface named CartesianCoord.
      1. The interface should declare two public methods, getX() and getY(), that take no arguments and return a value of type double.
      2. The interface should declare a public method named plus in terms of a parameter of type CartesianCoord. The plus() method should return a value of type CartesianCoord.
    2. Your program should include an interface named EuclideanCoord that extends CartesianCoord.
      1. The interface should declare a public method, getZ(), that takes no arguments and returns a value of type double.
      2. The interface should declare a public method, plus(), that takes one argument of type EuclideanCoord and returns a value of type EuclideanCoord.
  2. Your program should include a class named Point2D that implements CartesianCoord.
    1. The class should have a constructor method that is defined in terms of two parameters of type double. The first value passed to the constructor should be the object’s x-value and the second value should be its y-value. Both values should be stored in private final instance variables. (You choose the names of the parameters and the names of the instance variables.)
    2. The class should override the toString() method. The String value returned should be a left parenthesis character followed by a comma-separated ordered list of the object’s x- and y-values, followed by a right parenthesis. The x-value should come first followed by the y-value.
    3. The class should implement the methods specified by the CartesianCoord interface.
      1. The getX() and getY() methods should return the value of the corresponding instance variable.
      2. The plus() method should return a new Point2D object whose x- and y-values are the sum of two other objects’ values; for example, the x-value of the new object should be the sum of the x-value of the object whose plus() method is being called and the x-value of the object passed to the method.
  3. Your program should include a class named Point3D that extends Point2D and implements EuclideanCoord.
    1. The class’s getZ() and plus() methods should be analogous to the corresponding Point2D methods.
    2. The class should override the toString() method. The String value returned should be just like the value returned by the Point2D class except it should include another comma followed by the object’s z-value after the comma-separated x- and y-values.
  4. Your program should include a public class named AddPoints, and that class should have a main method.
    1. The main method should declare and initialize three variables. (You choose the names of the variables.) One variable should be of type CartesianCoord and the other two should be of type EuclideanCoord.
    2. The main method should add a CartesianCoord object and a EuclideanCoord object using the plus() method of one of the two objects, and it should output the toString() value of the object returned. The main method should also output the toString() value of an object returned by adding two objects of type EuclideanCoord.