Pages

Exam2-APCSA-17-18

This exam consists of 15 multiple choice questions and one coding exercise. 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 coding exercise is worth 25 points. Partial credit will be awarded. The following grading scale, which is analogous to the scale use for grading programming assignments, will be used when scoring the coding exercise.

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

Please, do your own work. And Don’t Panic!

(Some of these questions should look familiar to you.)

IMPORTANT NOTE:

Java’s + operator behaves in ways that are sometime unexpected.

  • If both operands are of type char, the values are converted to integers and the integers are added, yielding an integer.
  • If one operand is of type char and the other is of type String, then the character is converted to value of type String (where the converted value is a string consisting of the original character) and the two strings are concatenated, yielding another string.





What are the two possible values of a Java variable that is of type boolean?

  1. high, low
  2. one, zero
  3. 1, 0
  4. yes, no
  5. good, bad
  6. none of the above

Which of the following primitive Java data types are used to represent real numbers (i.e. numbers that may have digits to the right of the decimal point)?

  1. byte
  2. short
  3. int
  4. long
  5. double
  6. float

The authors of Computer Science - An Interdisciplinary Approach formally define the idea of a data type as 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 methods
  5. functions and parameters
  6. none of the above

Which of the following are not Java reserved words used to specify a primitive data type?

  1. Integer
  2. String
  3. character
  4. boolean
  5. Boolean
  6. double








The following program outputs the digits 5011. Why?

public class Plus {
  public static void main(String[] args) {
    System.out.print(1 + '1');
    System.out.print("1" + '1');
  }
}
  1. When converted to a string, the value of the number 2 is "50".
  2. When converted to an integer, the value of the character '1' is 50.
  3. When converted to an integer, the value of the character '1' is 49.
  4. When one operand is a character and the other is an integer, the + operator converts the character to an integer and then adds the integers, yielding an integer.
  5. When one operand is a character and the other is a string, the + operator concatenates values, yielding a string.
  6. The number 2 in decimal is represented by the digits 11 in binary.

Which statements about the following Java class are true?

public class Foo {
  public static String bar(String Java) {
    return Java;
  }
  public static void main(String[] args) {
    String 1 = "Hello, ";
    String 2 = Foo.bar("Java");
    System.out.print(1 + 2);
  }
}
  1. Java and args are names of parameters.
  2. The code cannot be compiled, because Java is not a valid name for an identifier.
  3. The code cannot be compiled, because identifiers cannot begin with a number.
  4. The code cannot be compiled, because identifiers cannot contain digits.
  5. When executed, the program will print the string "Hello, Java".
  6. The argument passed to the method System.out.print is the string "Hello, Java".


Which of the following Java code snippets contain examples of literals?

  1. int x;
  2. int x = 0;
  3. public static void main(String[] args) {
  4. if (letter > 'a') {
  5. System.out.print("Hello, World");
  6. none of the above




Which of the following statements are true about the following line of Java code?

String a, b, c = "undefined";
  1. The string "undefined" cannot be used to initialize the variable c, because undefined is a reserved word.
  2. Three variables are declared and all three are initialized to the value "undefined".
  3. Three variables are declared but only one of them is initialized.
  4. The code will fail to compile, because only one variable can be declared at a time in a single declaration statement.
  5. The code will fail to compile, because variable initialization statements must follow variable declaration statements; the two types of statements cannot be combined.
  6. none of the above

For what values of a, b, and c does the following expression evaluate to true?

(a && b) || (!a && c)
  1. a is true; b is true; c is true;
  2. a is true; b is false; c is true;
  3. a is false; b is false; c is true;
  4. a is false; b is true; c is false;
  5. a is true; b is false; c is false;
  6. a is true; b is true; c is false;

What is the value of the following boolean expression?

((true || !false) == (false && !true))
  1. true
  2. false
  3. 1
  4. 0
  5. undefined
  6. none of the above

Which of the following identifiers exemplify the coding style known as camel case?

  1. cc
  2. CC
  3. CamelCase
  4. camel_case
  5. Camel_Case
  6. camelCase
  7. none of the above


Which of the following Java statements produces a result that is different from the results produced by the other statements?

  1. System.out.println("\"");
  2. System.out.println('\"');
  3. System.out.println('"');
  4. System.out.print("\"\n");
  5. System.out.print('"' + "\n");
  6. System.out.print('"' + '\n');
  7. none of the above

Which of the following statements about the following line of Java code are true?

System.out.print((char)34);
  1. A program that contains this line of code will fail to compile.
  2. A program that contains this line of code will produce a run-time error.
  3. A program that contains this line of code will print the number 34.
  4. A program that contains this line of code will print the character that corresponds to the number 34.
  5. This line of code contains an explicit cast.
  6. This line of code contains an implicit cast.
  7. none of the above

Which of the following are true about Donald Ervin Knuth?

  1. Knuth is tall and athletic and was a star player on his high school basketball team.
  2. Knuth’s mother worked in real estate and never retired.
  3. Knuth’s first published paper about The Potrzebie System of Weights and Measures was published in Scientific American magazine.
  4. Knuth was the youngest student ever to graduate from his high school.
  5. Knuth never heard of calculus until he got to college.
  6. Knuth’s college calculus teacher would assign, say, the odd number problems in the textbook. Knuth would also do the even number problems and the supplementary problems.

What is the title of the famous series of books about computer programming written by Donald Ervin Knuth?

  1. The Elements of Computer Programming
  2. Computer Science - An Interdisciplinary Approach
  3. The Science of Computer Programming
  4. The Art of Computer Programming
  5. Things a Computer Scientist Rarely Talks About
  6. none of the above






Coding Exercise

On a blank sheet of paper, write your name in the upper right hand corner of the page as usual — last name on top, preferred name in parentheses below. Then write a Java program meets the following requirements. You should be able to fit your program on one sheet of paper. (You may use both sides of the paper.) When you turn in your exam, staple the page containing your code to the back of this exam.

  1. It must be possible to successfully compile and execute your program when the source code is saved in a file named FamousFirstWords.java.
  2. When executed using the command java MobyDick, your program must output the text:
    "Call me Ishmael."
  3. When executed using the command java FinnegansWake, your program must output the text:
    "riverrun, past Eve and Adam's, from swerve of shore to bend
    of bay, brings us by a commodius vicus of recirculation back to
    Hawth Castle and Environs."
    
  4. When executed using the command java FamousFirstWords, your program must output the text:
    Finnegans Wake begins in the middle of a sentence with the words
    "riverrun, past Eve and Adam's, from swerve of shore to bend
    of bay, brings us by a commodius vicus of recirculation back to
    Hawth Castle and Environs."
    
    The first sentence of Moby Dick, or The Whale is "Call me Ishmael."
    

You are strongly encouraged to practice writing your program on a piece of scratch paper before attempting to write the final version of your program. When you are satisfied with your preliminary work, plan out and neatly write the final version of your program on a clean sheet of paper. Consider writing your program in pencil in case you need to erase mistakes; just be sure to erase thoroughly. Correctness, neatness, style, clarity, and simplicity are important factors that will be considered when your work is subjectively evaluated.

Hint:

Your program should be composed of three classes. The main method of your public class should contain calls to methods of your other two classes.

A Solution
/**
 * The <code>MobyDick</code> class consists of a main method
 * that prints the famous first words of Hermine Melville's
 * magnum opus, <i>Moby Dick or The Whale</i>.
 */
class MobyDick {
  public static void main(String[] args) {
    System.out.println("\"Call me Ishmael.\"");
  }
}

/**
 * The <code>FinnegansWake</code> class consists of a main
 * method that prints the famous first words of James
 * Joyce's ultimate masterpiece, <i>Finnegans Wake</i>. The
 * first word is not capitalized, because the story begins
 * in the middle of a sentence that actually begins at the
 * end and loops back around to the beginning.
 */
class FinnegansWake {
  public static void main(String[] args) {
    System.out.print("\"riverrun, past Eve and Adam's, ");
    System.out.println("from swerve of shore to bend");
    System.out.print("of bay, brings us by a commodius vicus ");
    System.out.println("of recirculation back to");
    System.out.println("Hawth Castle and Environs.\"");
  }
}

/**
 * The <code>FamousFirstWords</code> class consists of a main
 * method that introduces the reader to the famous first words
 * of two of the greatest novels written in the English language.
 */
public class FamousFirstWords {
  public static void main(String[] args) {
    System.out.print("Finnegans Wake begins in the middle of ");
    System.out.println("a sentence with the words");
    FinnegansWake.main(args);
    System.out.println();
    System.out.print("The first sentence of Moby Dick, or ");
    System.out.print("The Whale is ");
    MobyDick.main(args);
  }
}