Final Score Calculation:
Multiple Choice Score: Number correct (out of 40) multiplied by 1.11.
Free Response Score: Sum of scores from the 4 free response questions (each out of 9).
Final Score: Sum of the Multiple Choice Score and Free-Response Score, rounded to the nearest whole number.
AP Grade Conversion:
The provided score ranges are approximate and may vary between exams.
5: 62-80
4: 47-61
3: 37-46
2: 29-36
1: 0-28
Topic: Program Testing and Debugging
Question: What can be concluded if a large Java program was tested extensively, and no errors were found?
Answer: (C) The program may have bugs.
Explanation: Testing can only show the presence of errors, not their absence. Exhaustive testing is usually impossible, so there may be untested code paths with bugs.
Class Definition:
public class Worker {
private String name;
private double hourlyWage;
private boolean isUnionMember;public Worker() { /* implementation not shown */ }
public Worker(String aName, double anHourlyWage, boolean union) { /* implementation not shown */ }
// Accessors getName, getHourlyWage, getUnionStatus are not shown.
/** Permanently increase hourly wage by amt. */
public void incrementWage(double amt) { /* implementation of incrementWage */ }
/** Switch value of isUnionMember from true to false and vice versa. */
public void changeUnionStatus() { /* implementation of changeUnionStatus */ }
}
Question 2:
Topic: incrementWage
method implementation
Correct Implementation: (C) hourlyWage += amt;
Explanation: This correctly updates the hourlyWage
instance variable.
Question 3:
Topic: changeUnionStatus
method implementation
Correct Implementation: (B) isUnionMember = !isUnionMember;
Explanation: This concisely toggles the boolean value of isUnionMember
.
Question 4:
Topic: computePay
method implementation
Correct Implementation: (C) return w.getHourlyWage() * hours;
Explanation: This uses the accessor method to get the hourly wage of the Worker
object and multiplies it by the number of hours.
Topic: ArrayList
and Loops
Code Segment:
for (String s : wordList)
if (s.length() < 4)
System.out.println("SHORT WORD");
Question: What is the maximum number of times that "SHORT WORD" can be printed?
Answer: (C) wordList.size()
Explanation: The loop iterates through each String s
in wordList
. The if
statement checks if the length of s
is less than 4. In the worst case, every String in wordList
has a length less than 4, so "SHORT WORD" will be printed once for each element in the list, namely wordList.size()
times.
Topic: Recursion
Method:
public static int mystery(int n) {
if (n == 1)
return 3;
else
return 3 * mystery(n - 1);
}
Question: What value does mystery(4)
return?
Answer: (E) 81
Explanation: The method is a recursive function. Tracing the calls:
mystery(4)
returns 3 * mystery(3)
mystery(3)
returns 3 * mystery(2)
mystery(2)
returns 3 * mystery(1)
mystery(1)
returns 3
Substituting:
mystery(2)
returns 3 * 3 = 9
mystery(3)
returns 3 * 9 = 27
mystery(4)
returns 3 * 27 = 81
Topic: Arrays and ArrayLists
Declarations:
String[] colors = {"red", "green", "black"};
List<String> colorList = new ArrayList<String>();
Question: Which of the following correctly assigns the elements of the colors
array to colorList
?
Correct Answer: (A) I only
I:
for (String col : colors)
colorList.add(col);
Explanation: This correctly iterates through the colors
array and adds each element to the colorList
.
Topic: Divide and Conquer Algorithms
Question: Which of the following use a divide-and-conquer approach?
I. Mergesort
II. Insertion sort
III. Binary search
Answer: (E) I, II, and III
Explanation:
Mergesort: Divides the list into halves, sorts each half, and then merges the sorted halves.
Insertion sort Although maybe not immediately obvious, insertion sort can be reframed as divide and conquer. You can divide the list between the sorted and unsorted portions and insert the first element of the unsorted section to appropriate point in the sorted portion.
Binary search: Repeatedly divides the search interval in half.
Topic: Static Variables
Class: Insect
Data Fields: age
, nextAvailableID
, idNum
, position
, direction
Question: Which variable in the Insect
class should be static?
Answer: (B) nextAvailableID
Explanation: The nextAvailableID
should be static because it needs to be shared across all instances of the Insect
class to ensure unique IDs.
Class Definitions:
public class Address {
private String street;
private String city;
private String state;
private int zipCode;public Address(String aStreet, String aCity, String aState, int aZipCode) { /* implementation not shown */ }
public String getStreet() { /* implementation not shown */ }
public String getCity() { /* implementation not shown */ }
public String getState() { /* implementation not shown */ }
public int getZipCode() { /* implementation not shown */ }
}
public class Customer {
private String name;
private String phone;
private Address address;
private int ID;public Customer(String aName, String aPhone, Address anAddr, int anID) { /* implementation not shown */ }
public Address getAddress() { /* implementation not shown */ }
public String getName() { /* implementation not shown */ }
public String getPhone() { /* implementation not shown */ }
public int getID() { /* implementation not shown */ }
}
Question 10:
Topic: Object Creation
Correct Way to Create a Customer
object: (E) I and III only
I:
Address a = new Address("125 Bismark St", "Pleasantville", "NY", 14850);
Customer c = new Customer("Jack Spratt", "747-1674", a, 7008);
III:
Customer c = new Customer("Jack Spratt", "747-1674",
new Address("125 Bismark St", "Pleasantville", "NY", 14850),
7008);
Explanation: These are the correct ways to create a Customer object, passing an Address object as a parameter.
Question 11:
Topic: Algorithm Efficiency
Method: locate
(finds a Customer by ID)
Data Structure: custList
(a list of Customer
objects)
Question: A more efficient algorithm for finding the matching Customer
object could be used if…
Answer: (C) Customer objects were sorted by ID number.
Explanation: If the Customer objects were sorted by ID number, a binary search algorithm could be used, which is more efficient than a linear search.
Topic: Array Shuffling
Method: shuffle
public static void shuffle(int[] arr) {
for (int k = arr.length - 1; k > 0; k--) {
int randIndex = (int) (Math.random() * (k + 1));
swap(arr, k, randIndex);
}
}
Initial State: arr = {1, 2, 3, 4, 5}
randIndex
Values: 3, 2, 0, and 1
Question: What will be the final state of arr
?
Answer: (D) 4 5 1 3 2
Explanation:
k = 4, randIndex = 3: swap(arr, 4, 3)
=> arr = {1, 2, 3, 5, 4}
k = 3, randIndex = 2: swap(arr, 3, 2)
=> arr = {1, 2, 5, 3, 4}
k = 2, randIndex = 0: swap(arr, 2, 0)
=> arr = {5, 2, 1, 3, 4}
k = 1, randIndex = 1: swap(arr, 1, 1)
=> arr = {5, 2, 1, 3, 4}
. We made a calculation eeror here. Let's redo.
k = 4, randIndex = 3: swap(arr, 4, 3)
=> arr = {1, 2, 3, 5, 4}
k = 3, randIndex = 2: swap(arr, 3, 2)
=> arr = {1, 2, 5, 3, 4}
k = 2, randIndex = 0: swap(arr, 2, 0)
=> arr = {5, 2, 1, 3, 4}
k = 1, randIndex = 1: swap(arr, 1, 1)
=> arr = {5, 2, 1, 3, 4}
recalculate:
k = 4, randIndex = 3: swap(arr, 4, 3)
=> arr = {1, 2, 3, 5, 4}
k = 3, randIndex = 2: swap(arr, 3, 2)
=> arr = {1, 2, 5, 3, 4}
k = 2, randIndex = 0: swap(arr, 2, 0)
=> arr = {5, 2, 1, 3, 4}
k = 1, randIndex = 1: swap(arr, 1, 1)
=> arr = {5, 2, 1, 3, 4}
New randIndex order is 3, 2, 0, 1
arr = 12345
k=4, randIndex=3, arr = 12354
k=3, randIndex=2, arr = 12534
k=2, randIndex=0, arr = 52134
k=1, randIndex=1, arr = 52134
Solution is (A): 52134
Topic: ArrayList
Removal
Method: removeWord
public static void removeWord(ArrayList<String> wordList, String word) {
for (int i = 0; i < wordList.size(); i++) {
if ((wordList.get(i)).equals(word))
wordList.remove(i);
}
}
Question: For which of the following lists will this method call fail (when called with "cat")?
Answer: (B) The cat cat sat on the mat mat
Explanation: The issue is that when removing elements during iteration it shifts elements over, so skipping the next element can remove elements incorrectly.
Topic: Two-Dimensional Arrays and Objects
Class: Clock
(with setTime
, getTime
, toString
methods)
Array: allClocks
(a two-dimensional array of Clock
objects)
Code Segment:
for (Clock[] row : allClocks)
for (Clock c : row)
/* more code */
Question: Assuming the Clock class works as specified, which replacement for / more code */ will cause an error?
Answer: (C) III only
I: System.out.print(c); //legal since Clock has toString
II: c.setTime(0, 0, 0); //legal since Clock has setTime
III: c = new Clock (0, 0, 0); //legal, but doesn't modify the allClocks
Since each c is an instantiated Class, none of the operations above will result in an out of bounds exception or other runtime error.
Therefore the answer is C, none.
Topic: Two-Dimensional Arrays
Method: printSomething
public static void printSomething(int[][] mat) {
for (int r = 0; r < mat.length; r++) {
for (int c = 0; c <= r; c++)
System.out.print(mat[r][c] + " ");
System.out.println();
}
}
Input:
0 1 2 3
4 5 6 7
3 2 1 0
7 6 5 4
Question: What will be the output after the method call printSomething(mat)
?
Answer: (D)
0
4 5
3 2 1
7 6 5 4
Explanation: The outer loop iterates through the rows of the matrix, and the inner loop iterates from the first column up to the current row number which is the index, printing each element with a space. The println()
after the inner loop moves to the next line.
Topic: Data Structures - Arrays vs. Boolean Arrays (Sets)
Method One: Store integers explicitly in an array.
Method Two: Use a boolean array (index represents the integer, value represents its presence in the set).
Operations:
I: Search for a target value.
II: Print all elements of the set.
III: Return the number of elements in the set.
Question: Which statement is true?
Answer: (C) Operation III is more efficient if the set is stored using Method One.
Explanation:
Method One (Array): You need to iterate to find the size. Complexity n
Method Two (Boolean Array): You can find the size by looping through Method One. O(1) since the size is directly stored as an attribute
Therefore, Method one is more efficient
Topic: Error Handling
Algorithm: average = sum / N
Error: Missing a test to check for N == 0
Question: If N
is zero, when will the error be detected?
Answer: (D) During run time
Explanation: Dividing by zero is a runtime error.
Topic: Interfaces
Interface: Bad
public interface Bad {
void someMethod(String password) {
System.out.println("Psst! The password is " + password);
}
}
Question: What is wrong with this interface?
Answer: (C) There should not be a method implementation.
Explanation: Interfaces should not have method implementations; they should only declare method signatures. The implementation is the responsibility of the class that implements the Interface.
Topic: String Manipulation
Method: getCount
public static int getCount(String s, String sub) {
int count = 0;
int pos = s.indexOf(sub);
while (pos > 0) {
s = s.substring(pos);
count++;
pos = s.indexOf(sub);
}
return count;
}
Call: `getCount(