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
- Write your last name in the upper right hand corner of each page of this exam.
- Below your last name, write the name you prefer to be called in class in parentheses.
- For each multiple choice question, circle the letter of the correct answer(s).
- Follow the instructions for the coding exercise that are provided in that section of the exam.
- 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.
Mark Points ++ 26 + 25 ✓+ 24 ✓ 23 ✓- 21 - 19 -- 17 0 0
and simple to express:
Err
and err
and err again
but less
and less
and less.—Piet Hein
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?
- values and operations
- ones and zeros
- names and values
- operations and operands
- methods and parameters
- none of the above
Correct: | 11 |
Incorrect: | 11 |
Which of the following Java Boolean expressions evaluate to true
?
true || false
true && false
(true || false) && (!true || !false)
(1 / 2 > 0)
(1.0 / 2 > 0)
(1.0 / 2.0 > 0)
Correct: | 17 |
Incorrect: | 5 |
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() ); } }
- T
- F
- FT
- FTT
- FTF
- FTFTT
Correct: | 1 |
Incorrect: | 21 |
Which of the following statements about Java constructor methods are true?
- Constructors may be invoked using either the keyword
new
, the keywordthis
, or the keywordsuper
. - The name of a constructor method must be the same as the name of the class it belongs to.
- The signature of a constructor method does not include a return type.
- The return type of a constructor method may be one of Java's eight primitive types.
- A class may have more than one constructor method.
- 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 |
Which of the following statements are true?
- I’m glad this exam doesn’t have any DEK questions.
- I would watch DEK videos even if they weren’t assigned.
- DEK is boring.
- I wish I had my own copies of TAOCP. (I once had a dream about a signed first edition.)
- I didn’t know that one of Piet Hein’s grooks is engraved on the entryway to DEK’s house.
- This question won’t compile, because DEK and TAOCP aren’t defined.
Correct: | 1 |
Incorrect: | 21 |
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.
Step | p | c | r | i | j | k |
---|---|---|---|---|---|---|
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 |
Answer (program output)
Correct: | 20 |
Incorrect: | 2 |
Correct: | 8 |
Incorrect: | 14 |
What does the following program print?
public class Geometry { public static void main(String[] args) { int x = 3, y = 2; double[][] plane = { {x, y}, {x / y, x % y} }; for (double[] line : plane) { for (double point : line) { System.out.print(point + " "); } } } }
3.0 2.0 1.0 0.0
3.0 2.0 1.0 1.0
3.0 2.0 1.5 1.0
3.0 1.0 2.0 0.0
3.0 1.0 1.5 1.0
3.0 1.5 2.0 1.0
Correct: | 2 |
Incorrect: | 20 |
What does the following program print, if anything?
public class YetAnotherRecursiveProgram { public static void main(String[] args) { System.out.print("a"); if (args != null) { args = null; main(args); System.out.print("b"); } } }
- a
- ab
- aba
- aab
- The program prints the string ab an indefinite number of times.
- The program prints an error message.
Correct: | 9 |
Incorrect: | 13 |
Which of the statements below would cause a compile-time error if it was appended to the end of the following main method?
public class Matrix { public static void main(String[] args) { int[][] matrix = new int[2][2]; int[] array = new int[3]; matrix[1] = array; /* append statement here */ } }
int a = matrix[0][0];
int b = matrix[0][1];
int c = matrix[1][1];
int d = matrix[1][2];
int e = matrix[2][2];
int f = matrix[2][d];
Correct: | 11 |
Incorrect: | 11 |
Which of the statements below would not cause a compile-time error but would trigger an exception (a run-time error) if the statement was inserted at the beginning of the following main method and an attempt was made to compile and execute the program?
public class Foo { public static int bar(int[] a, int n) { n++; return a[1] % n; } public static void main(String[] args) { /* insert statement here */ Foo.bar(values, values[0]); } }
int[] values = { 0 };
int values[] = { 0, 0 };
int[] values = { -1, -1 };
int[] values = { -1, 0 };
int[] values = null;
int[] values = new int[2];
Correct: | 0 |
Incorrect: | 22 |
Which of the following are valid Java statements?
int x = null;
Integer x = null;
String x = null;
String[] x = null;
char x = null;
Boolean x = null;
Correct: | 8 |
Incorrect: | 14 |
Correct: | 9 |
Incorrect: | 13 |
Correct: | 19 |
Incorrect: | 3 |
Correct: | 12 |
Incorrect: | 10 |
Coding Exercise (50 points)
Write a Java program that satisfies the following requirements:
- Your program should include two interfaces.
- Your program should include an interface named
CartesianCoord
.- The interface should declare two public methods,
getX()
andgetY()
, that take no arguments and return a value of typedouble
. - The interface should declare a public method named
plus
in terms of a parameter of typeCartesianCoord
. Theplus()
method should return a value of typeCartesianCoord
.
- The interface should declare two public methods,
- Your program should include an interface named
EuclideanCoord
that extendsCartesianCoord
.- The interface should declare a public method,
getZ()
, that takes no arguments and returns a value of typedouble
. - The interface should declare a public method,
plus()
, that takes one argument of typeEuclideanCoord
and returns a value of typeEuclideanCoord
.
- The interface should declare a public method,
- Your program should include an interface named
- Your program should include a class named
Point2D
that implementsCartesianCoord
.- 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 inprivate final
instance variables. (You choose the names of the parameters and the names of the instance variables.) - The class should override the
toString()
method. TheString
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. - The class should implement the methods specified by the
CartesianCoord
interface.- The
getX()
andgetY()
methods should return the value of the corresponding instance variable. - The
plus()
method should return a newPoint2D
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 whoseplus()
method is being called and the x-value of the object passed to the method.
- The
- The class should have a constructor method that is defined in terms of two parameters of type
- Your program should include a class named
Point3D
that extendsPoint2D
and implementsEuclideanCoord
.- The class’s
getZ()
andplus()
methods should be analogous to the correspondingPoint2D
methods. - The class should override the
toString()
method. TheString
value returned should be just like the value returned by thePoint2D
class except it should include another comma followed by the object’s z-value after the comma-separated x- and y-values.
- The class’s
- Your program should include a public class named
AddPoints
, and that class should have a main method.- 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 typeEuclideanCoord
. - The main method should add a
CartesianCoord
object and aEuclideanCoord
object using theplus()
method of one of the two objects, and it should output thetoString()
value of the object returned. The main method should also output thetoString()
value of an object returned by adding two objects of typeEuclideanCoord
.
- The main method should declare and initialize three variables. (You choose the names of the variables.)
One variable should be of type