Pages

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

May your day be touched
by a 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 false?

(There is more than one correct answer.)

  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 integers?

(There is more than one correct answer.)

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

Q3 = questions[2];

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

(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 int? (An int is a 32 bit value.)

(This is more than one correct answer.)

  1. 32!
  2. (232) - 1
  3. (232)/2
  4. 2(32 - 1)
  5. (231) - 1
  6. ((232)/2) - 1

Q6 = questions[5];

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

(There is more than one correct answer.)

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

Which of the following 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.)
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 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.
  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.
Sample Answers
public class ProgramA {
    public static void swap(String[] s, int i, int j) {
        String si = s[i];
        String sj = s[j];
        s[i] = sj;
        s[j] = si;
    }
    public static void reverse(String[] s, int i, int j) {
        swap(s, i, j);
        i++;
        j--;
        if (i < j)
            reverse (s, i, j);
    }
    public static void main(String[] args) {
        reverse(args, 0, args.length - 1);
        for (String s : args)
            System.out.println(s);
    }
}

class ProgramB {
    public static int pow(int x, int y) {
        if (y == 0)
            return 1;
        if (y < 0) 
            return 1 / pow(x, -y);
        return x * pow(x, y - 1);
    }
    public static int powPow(int x, int y, int z) {
        /*
         * NOTE: The prompt is ambiguous!
         * Which is desired: 2^(3^4) or (2^3)^4 ?
         */
        return pow(x, pow(y, z));    // if 2^(3^4) is desired, or
        // return pow(pow(x, y), z); // if (2^3)^4 is desired
    }
    public static void main(String[] args) {
        System.out.print(powPow(2,3,4));
    }
}