Pages

Exam4-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

Don’t Panic!

(This won’t hurt a bit.)

An Old Celtic Blessing

May the blessing of light be on you—
light without and light within.
May the blessed sunlight shine on you
and warm your heart
till it glows like a great peat fire.

Q1 = questions[0];

Which of the following Java expressions evaluate to true?

(There are 4 correct answers.)

  1. true
  2. !false
  3. true || false
  4. true && false
  5. !(true || false)
  6. !(true && false)

Q2 = questions[1];

Which of the following primitive Java data types are used to represent floating point (i.e. decimal) numbers?

(There are 2 correct answers.)

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

Q3 = questions[2];

Which of the following statements about the javac program are true?

(There are 3 correct answers.)

  1. You can use the program to compile a Java program.
  2. You can use the program to execute a Java program.
  3. You can use the program to create documentation for a Java program.
  4. The program can produce .class files as output.
  5. The program tries to run a Java program when you pass it the name of a Java class.
  6. The program tries to compile a Java program when you pass it the name of a file with a .java filename extension.

Q4 = questions[3];

Which of the following statements about the java program are true?

(There are 2 correct answers.)

  1. You can use the program to compile a Java program.
  2. You can use the program to execute a Java program.
  3. You can use the program to create documentation for a Java program.
  4. The program can produce .class files as output.
  5. The program tries to run a Java program when you pass it the name of a Java class.
  6. The program tries to compile a Java program when you pass it the name of a file with a .java filename extension.

Q5 = questions[4];

Assuming the main method of a program is defined in terms of a parameter named args, which of the following statements are true?

(There are 3 correct answers.)

  1. The value of args can be a value of type String.
  2. If the main method is called, either by another method or by itself, then the value of args can be null.
  3. The value of args can be an array of values of type String.
  4. The value of args can be an array of values of type int.
  5. The value of args can be a two dimensional array of values, i.e. an array of arrays.
  6. The value of args can change as the program executes.

Q6 = questions[5];

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

(There are 3 correct answers.)

  1. String s = "abc";
  2. int i = 0;
  3. if (x > y) {
  4. int n;
  5. System.out.print(s);
  6. 3 % 2

Q7 = questions[6];

What happens when you attempt to compile and execute the following program?

(There is 1 correct answer.)

public class Fore {
  public static void main(String[] args) {
    int[] a = {4, 3, 2, 1};
    System.out.print(a[4]);
  }
}

  1. The program prints 4.
  2. The program prints 1.
  3. The program prints null.
  4. The program fails to compile.
  5. The program produces a run-time error.
  6. None of the above.

Q8 = questions[7];

The following program outputs the digits 5011. Why?

(There are 3 correct answers.)

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 of the + operator is a character and the other is an integer, the character is first converted to an integer; then the two integer values are added, and the result is another integer.
  5. When one operand of the + operator is a character and the other is a string, the character is implicitly converted to a string, the two string values are concatenate, and the result is another string.
  6. The number 2 in decimal is represented by the digits 11 in binary.

Q9 = questions[8];

Which statements about the following Java class are not true?

(There are 4 correct answers.)

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".

Q10 = questions[9];

What does the following program print when it is executed?

(There is 1 correct answer.)

public class RecursiveProgram {
  public static void main(String args[]) {
    System.out.print("a");
    if (args != null) {
      System.out.print("b");
      args = null;
      main(args);
    }
  }
}
  1. a
  2. ab
  3. aba
  4. abab
  5. The program prints the string ab repeatedly an indefinite number of times.
  6. The program prints a run-time error message.

Q11 = questions[10];

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

(There is 1 correct answer.)
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

Q12 = questions[11];

What does the following program print?

(There is 1 correct answer.)

public class Points {
  public static void main(String args[]) {
    String s = "";
    int x = 3, y = 4; 
    int[][] a = { {x, y}, {2*x, 2*y} };
    int n = a[0].length;
    for (int i = 0; i < n; i++) {
      int m = a[i].length;
      for (int j = 0; j < m; j++) {
        s += a[i][j];
      }
    }
    System.out.print(s);
  }
}
  1. 2468
  2. 3468
  3. 3648
  4. 1248
  5. 102
  6. 21

Q13 = questions[12];

What does the following program print?

(There is 1 correct answer.)

public class Points {
  public static void main(String args[]) {
    int s = 0;
    int x = 3, y = 4; 
    int[][] a = { {x, y}, {2*x, 2*y} };
    int n = a[0].length;
    for (int i = 0; i < n; i++) {
      int m = a[i].length;
      for (int j = 0; j < m; j++) {
        s += a[i][j];
      }
    }
    System.out.print(s);
  }
}
  1. 2468
  2. 3468
  3. 3648
  4. 1248
  5. 102
  6. 21

Q14 = questions[13];

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

(There is 1 correct answer.)

  1. System.out.println("hello, world");
  2. System.out.println("hello," + " " + "world");
  3. System.out.println("h" + "e" + "l" + "l" + "o" + "," + " " + "w" + "o" + "r" + "l" + "d");
  4. System.out.println('h' + 'e' + 'l' + 'l' + 'o' + ',' + ' ' + 'w' + 'o' + 'r' + 'l' + 'd');
  5. System.out.print("hello, world\n");
  6. System.out.print("hello, " + "world" + '\n');

Q15 = questions[14];

Which of the following are true about Donald Ervin Knuth?

(There are 6 correct answers.)

  1. Although Knuth said he is glad he cannot prove that God exists, he also said that he believes in God.
  2. Knuth came to believe that his education boiled down to 50% mathematics and 50% writing skills.
  3. Knuth didn’t take his first calculus course until college. (His high school didn’t offer calculus.)
  4. The third computer program that Knuth wrote was a program that could learn to play tic-tac-toe.
  5. Knuth brought source code listings for two computer programs with him to study during his spare time while his family was at a summer camp.These programs had a big influence on his life.
  6. Knuth was in the unusual position of taking a class his sophomore year at Case for which he has written the textbook the previous summer!

Coding Exercise

Write a Java program that meets the following requirements.

  1. It should be possible to compile and execute your program if your code is saved in a file named Reversed.java.
  2. Your program should try to convert the values of all of the command-line arguments from strings to integers.
  3. The converted integers should be placed into an array.
  4. Your program should print the values of the array in reverse order, one value per line. For example, if the command line arguments are "1" "2" "3", your program should output:
    3
    2
    1
    

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. Consider writing your program in pencil in case you need to erase mistakes. (Erase thoroughly.) Correctness, neatness, style, clarity, and simplicity are important.