2015 AP Exam Practice Test Corrections
MC
Score: 24/39
Question 26
- Method does not change nums or the string
- The method is the same in the main start method.
- Original array will be printed and the original string will be printed.
Question 27
* Two for loops for sorting algorithm
* Keep track of variables on paper
* Moves number in the back to front
Question 28
* It is easy to see that the outer for loop will run 5 times.
* The j value is not changed outside of the for loop statement.
* The array has a length of 5.
* The inner loop iterates one less time every j value.
* 5 + 4 + 3 + 2 + 1 = 15
* Answer: 15/5
Question 29
* The code determines the amount of digits in yor integer.
* If the number is less than ten, it returns one, which means one integer
* Otherwise, it goes into the function until one is returned.
* It adds up all the 1s that were returned before.
Giving the number of time the number was divided by ten.
Question 30
* I does not work because if the customer buys more than 0 boxes, it would always print the maximum value. This can be fixed by putting an if else.
* II does works because it checks for the greatest number correctly and uses if else so the cost is only changed once.
* III does not work because if the cost is more than 5 or 10, the code will still end on the first statement.
Question 31
* Double array is initialized
* Follow through the for loop
* For ease, write down values as they change
* Keep a sketch of the board
* x is place diagonally after skipping one diagonal line.
Question 32
* The user inputs a major into the parameters
* Then with a for each loop, the code iterates through each student.
* They identify the student's major through the major getter method.
* With the getAge method, the age is added to the sum and the count has been increased.
* Sum and count then are used for the average
* Wrong answers include getting age and major without a getter
* Other wrong answers use array lists when not needed
Question 33
* I. works because it sets the minimum integer value to the maximum and changes it when it finds a bigger value.
* II. Boolean set to true and then set to false after first if statement. If statement was used for assigning the first value in the array to the max. Then, the code iterates through every element to find a bigger number.
* III. Sets first value of the array as max. Iterates though every element to find the max.
* All three work
Question 34
* Size of an array list has to be found by lostOfWords.size()
* Don't need -1 at the end.
* Condition: If we are not at the end of the list, add a comma
* Else, finish the code by adding a "}"
Question 35
* Dataset cut into half to find the target number
* If number is greater than mid, first half is ignored
* Start value gets moved to mid + 1
* Process starts again until target is found
Question 36
* As the loop iterates, the size is divided into two
* In order to reach 1, we have to half the array 11 times.
Question 37
* II works because it starts from the end and the counter goes back. With it, the words can be printed in reverse order upto the index in the parameter.
* III works because it moves items in the front to the back.
Question 38
* Returns the number of elements in numbers that are equal to val
* If statement adds one if the previous number is the same
* Recursive method.
Question 39
* The first for loop prints all the elements before switching them.
* The second for loop also prints out each element but all of them have been changed to Alex.
FRQ
Question 1
public static int arraySum(int[] arr){
int sum = 0;
for(int i = 0; i < arr.length; i++){
sum += arr[i];
}
return sum;
}
public static int[] rowSum(int[][] arr2D){
int[] arr = new int[arr2D.length];
for(int i = 0; i < arr2D.length; i++){
arr[i] = arraySum(arr2D[i]);
}
for(int i = 0; i < arr.length; i++){
}
return arr;
}
public static boolean isDiverse(int[][] arr2D){
int[] sums = rowSum(arr2D);
for(int i = 0; i < sums.length; i++){
for(int j = i + 1; j< sums.length; j++){
if(sums[i] == sums[j]){
return false;
}
}
}
return true;
}
public static void main(String[] args){
int[] arr = {2, 3, 5, 6, 4, 1};
int sum = arraySum(arr);
System.out.println("Sum");
System.out.println(sum);
System.out.println("");
int[] array = rowSum(arr2D).clone();
System.out.println("Array");
for(int i = 0; i < array.length; i++){
System.out.print(array[i] + " ");
}
System.out.println("");
System.out.println("");
Boolean diverse = isDiverse(arr2D);
System.out.println("Boolean");
System.out.println(diverse);
System.out.println("");
}
Question 2
public String word;
public HiddenWord(String m_word){
word = m_word;
}
public String getHint(String guess){
String hint = "";
for(int i = 0; i < guess.length(); i++){
if(guess.substring(i, i+1).equals(word.substring(i,i+1))){
hint += guess.substring(i, i+1);
}
else if(word.contains(guess.substring(i, i + 1))){
hint += "+";
}
else {
hint += "*";
}
}
return hint;
}
public static void main(String[] args){
HiddenWord hint = new HiddenWord("navodit");
System.out.println("Type in a word: ");
for(int i = 0; i < 4; i++) {
Scanner scan = new Scanner(System.in);
String guess = scan.nextLine();
String result = hint.getHint(guess);
System.out.println(result);
if(guess.equals(result)){
break;
}
}
}
Question 3
public class SparseArrayEntry {
// row and column index for this entry in the sparse array
private int row;
private int col;
// the value of this entry in the sparse array
private int value;
public SparseArrayEntry(int r, int c, int v){
row = r;
col = c;
value = v;
}
public int getRow(){
return row;
}
public int getCol(){
return col;
}
public int getValue(){
return value;
}
}
Question 4
public interface NumberGroup {
boolean contains(int i);
}
public class Range implements NumberGroup {
private int min;
private int max;
public Range(int min, int max) {
this.min = min;
this.max = max;
}
@Override
public boolean contains(int i) {
return this.min <= i && i <= this.max;
}
}
import java.util.Arrays;
import java.util.List;
public class MultipleGroups implements NumberGroup {
private List<NumberGroup> groupList;
public MultipleGroups(NumberGroup... groups) {
this.groupList = Arrays.asList(groups);
}
@Override
public boolean contains(int i) {
for (NumberGroup group : groupList)
if (group.contains(i))
return true;
return false;
}
}