Pages

Exam6-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!

A Celtic Blessing (annotated)

May your day be touched (again)
by (yet another) bit of Irish luck,
brightened by a song in your heart,
and warmed by the smiles
of the people you love.

Q1 = questions[0];

Which of the following Java expressions evaluate to true?

(There is more than one correct answer.)

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

Q2 = questions[1];

Which of the following Java data types can be cast (explicitly or implicitly) to the primitive type int?

(There is more than one correct answer.)

  1. byte
  2. short
  3. float
  4. char
  5. double
  6. String

Q3 = questions[2];

How many bits does Java use to store a value of type int?

(There is one correct answer.)

  1. 2
  2. 4
  3. 8
  4. 16
  5. 32
  6. 64

Q4 = questions[3];

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

(There is more than one correct answer.)

  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];

In Java, what is the maximum possible value of a variable of type byte? (A value of type byte is an 8 bit value.)

(This is one correct answer.)

  1. 82 - 1
  2. (28) - 1
  3. (28)/2
  4. 2(8 - 1)
  5. (28) - 1
  6. ((28)/2) - 1

Q6 = questions[5];

Which of the following Java expressions evaluate to a value of type int?

(There is more than one correct answer.)

  1. 1
  2. 1.0
  3. 4 / 3
  4. 4.0 / 3.0
  5. 4 % 3
  6. 4.0 % 3.0

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];

Which statements about the following program are true?

(There is more than one correct answer.)

public class Fib {
  private static int answer(String[] args) {
    int n = args.length;
    if (n < 2)
      return 1;
    String[] sMinus1 = new String[n - 1];
    String[] sMinus2 = new String[n - 2];
    return answer(sMinus2) + answer(sMinus1);
  }
  public static void main(String[] args) {
    System.out.println(answer(args));
  }
}
  1. This program contains an example of a recursive method.
  2. This is a classic example of a situation where recursion is the only option available to produce the desired output.
  3. You should be very wary of this program, because the amount of space used can pile up very quickly, as can the amount of time required for memory management.
  4. This program could produce a stack overflow exception.
  5. This program contains unreachable code.
  6. This program will not compile.

Q9 = questions[8];

Which statements about the following Java program are true? (Assume the value of args is not null.)

(There is more than one correct answer.)

public class Foo {
  private static int bar(int i) {
    if (i > 0)
      return 2 * bar(i - 1);
    return 1;
  }
  public static void main(String[] args) {
    System.out.print(bar(args.length));
  }
}
  1. The program won’t compile because it contains unreachable code.
  2. For some inputs, the program will not terminate because it contains a potential infinite loop.
  3. The value printed is always greater than zero.
  4. The value printed is always even.
  5. The value printed is always a power of 2.
  6. The value printed can be negative.

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.)
int a, b, c = null;
  1. The value null cannot be used to initialize a variable of type int.
  2. A program containing this statement will not compile.
  3. A program containing this statement may compile but will produce a run time error.
  4. This statement would be valid if the data type were String instead of int.
  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];

When Knuth and his wife Jill sailed to Europe on their honeymoon, what book did Knuth bring with him to read in odd moments.

(There is 1 correct answer.)

  1. Alan Turing: The Enigma, by Andrew Hodges
  2. The Psychology of Computer Programming, by Gerald M. Weinberg
  3. C Programming Language, by Brian W. Kernighan and Dennis M. Ritchie
  4. The Algebraic Theory of Context-Free Languages, by Noam Chomsky
  5. ALGOL 60 Implementation: The translation and use of ALGOL 60 programs on a computer, by Brian Randall
  6. Concrete Mathematics: A Foundation for Computer Science, by Ronald L. Graham et al.

Q13 = questions[12];

Which of the following describes how Knuth felt when he began his graduate studies in mathematics?

(There is 1 correct answer.)

  1. Knuth was over confident.
  2. Knuth was scared stiff.
  3. This is a trick question: Knuth didn't study mathematics in graduate school.
  4. This is a trick question: Knuth became the yougest professor at Stanford immediately after receiving his masters degree.
  5. The Knuth videos didn't mention anything about this.
  6. None of the above.

Q14 = questions[13];

Where did Knuth go to graduate school after being simultaneously awarded his bachelors and masters degrees?

(There is 1 correct answer.)

  1. Caltech
  2. Case
  3. Stanford
  4. MIT
  5. U.C. Berkeley
  6. Harvard

Q15 = questions[14];

Which of the following are true about a summer job Donald Ervin Knuth had while he was in college?

(There is more than one correct answer.)

  1. Knuth worked for Sun Microsystems.
  2. Knuth worked for Burroughs Corporation.
  3. Knuth developed a compiler.
  4. Knuth developed an operating system.
  5. Knuth used the money he earned to get married.
  6. Knuth used the money he earned to buy his first car.

Coding Exercise

Do one of the following two coding exercises. 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.

Option A. Write a Java program that meets the following requirements.

  1. Your program’s main method should be in a public class named ProgramA.
  2. Your program should include a method called swap that is defined in terms of three parameters:
    1. The first parameter should be a variable of type array of strings named s.
    2. The second parameter should be a variable of type int named i.
    3. The third parameter should be a variable of type int named j.
  3. The method swap should exchange the values at index positions i and j in the array s.
  4. Your program should include a method called reverse that is defined in terms of three parameters:
    1. The first parameter should be a variable of type array of strings named s.
    2. The second parameter should be a variable of type int named i.
    3. The third parameter should be a variable of type int named j.
  5. The method reverse should reverse the sequence of values in s between the index positions i and j inclusive.
  6. The method reverse should call swap; it should also call itself recursively.
  7. Your program should use the method reverse to reverse the order of the elements in the array of strings (typically called args) containing the program’s command-line arguments.
  8. Your program should print the reversed values, one value per line.

Option B. Write a Java program that meets the following requirements.

  1. Your program’s main method should be in a public class named ProgramB.
  2. Your program should include a method called pow that is defined in terms of two parameters:
    1. The first parameter should be a variable of type int named x.
    2. The second parameter should be a variable of type int named y.
  3. The method pow should return the result of raising the value x to the power y.
  4. The method pow should call itself recursively.
  5. Your program should include a method called powPow that is defined in terms of three parameters:
    1. The first parameter should be a variable of type int named x.
    2. The second parameter should be a variable of type int named y.
    3. The third parameter should be a variable of type int named z.
  6. The method powPow should return the result of raising the value x to the power (y raised to the power z): for example, if x is 2, y is 3, and z is 4, then powPow(2, 3, 4) should return the value 281 since 34 is 81.
  7. The method powPow should call the method pow.
  8. Your program should call powPow and print the value of 2 raised to the power (3 raised to the power 4).