# 15.4. Practice Exam 4 for the AP CS A Exam¶

The following 20 questions are similar to what you might see on the AP CS A exam. Please answer each to the best of your ability.

Click the button when you are ready to begin the exam, but only then as you can only take the exam once. Click on the button to go to the next question. Click on the button to go to the previous question. Use the number buttons to jump to a particular question. Click the button to pause the exam (you will not be able to see the questions when the exam is paused). Click on the button after you have answered all the questions. The number correct, number wrong, and number skipped will be displayed.

15-4-1: Consider the following partial class definitions. Which of the constructors shown below (I, II, and III) are valid for C2?

```public class C1 {
private int num;
private String name;

public C1(int theNum) {
num = theNum;
}

public C1(String theName) {
name = theName;
}
// other methods not shown
}

public class C2 extends C1 {
// methods not shown
}

Possible constructors
I.   public C2 () { }
II.  public C2 (int quan) {super (quan); }
III. public C2 (String label) { super(label); }
```
• All three are valid
• If there is not a call to super as the first line in a child class constructor then super() is automatically added. However, this will cause a problem if the parent class does not have a no argument constructor.
• II only
• While II is valid so is another choice.
• III only
• While III is valid so is another choice.
• II and III
• Since C1 has constructors that take just an int and just a String both of these are valid.
• None are valid
• C2 constructors can call C1 constructors using the super keyword. In fact this call is automatically added to C2 constructors as the first line in any C2 constructor if it isn't there.

15-4-2: The Boolean expression (x==y && !(x==y)) || ( x!=y && !(x!=y)) can be simplified to which of the following?

• x != y
• If we assume that x is not equal to y then the expression is (false && true) || (true && false) which is false.
• x == y
• If we assume that x == y is the same than using it in the full expression should return true. But, if x is equal to y you would get (true && false) || (false && true) which is false.
• true
• How can this be true? Remember that && requires both expressions to be true in order to return true. You can think of (x==y && !(x==y)) as A && !A which is always false. You can think of ( x!=y && !(x!=y) as B && !B which is always false.
• false
• This can be simplified to (A && !A) || (B && !B) which is (false || false) which is false. You can think of (x==y && !(x==y)) as A && !A which is always false. You can think of ( x!=y && !(x!=y) as B && !B which is always false.
• x < y
• Since this expression is only about equality how could this be true?

15-4-3: Which of the following could be used to replace the missing code so that the method sort will sort the array a in ascending order?

```public static void sort(int[] a) {
int maxCompare = a.length - 1;
int savedIndex = 0;
int numSteps = 0;
int temp = 0;

for (int i = maxCompare; i > 0; i--) {
savedIndex = i;

for (int j = i - 1; j >= 0; j--) {
/* missing code */
}

temp = a[i];
a[i] = a[savedIndex];
a[savedIndex] = temp;
}
}
```
• if (a[savedIndex] > a[j]) { j = savedIndex; }
• Should j be set to the savedIndex?
• if (a[j] > a[savedIndex]) { savedIndex = j;}
• This is a selection sort that is starting at end of the array and finding the largest value in the rest of the array and swapping it with the current index.
• if (a[j] < a[savedIndex]) { savedIndex = j; }
• This would be correct if this was starting at index 0 and finding the smallest item in the rest of the array, but this starts at the end of the array instead and finds the largest value in the rest of the array.
• if (a[j] > a[savedIndex]) { j = savedIndex;}
• Should j be set to the savedIndex?
• if (a[j] == a[savedIndex]) { savedIndex = j; }
• Why would you want to change the savedIndex if the values are the same?

15-4-4: Which of the following statements about interfaces is (are) true?

```I.  One interface can inherit from another
II.  All methods declared in an interface are abstract methods
(can’t have a method body).
III. All methods declared in an interface are public methods.
```
• II only
• Methods in an interface are abstract, but more of these choices are correct.
• III only
• Methods in an interface are public, but more of these choices are correct.
• I and II only
• Can you declare private methods in an interface?
• I, II, and III
• One interface can inherit from another and the methods in an interface are public and abstract.
• I only
• One interface can inherit from another, but more of these choices are correct.

15-4-5: Consider the following declarations. If matrix is initialized to be: { {-1, -2, 3},{4, -5, 6} }. What will the values in matrix be after changeMatrix(matrix) is called?

```int[][] matrix = new int;

public static void changeMatrix(int[][] matrix )
{
for (int row = 0; row < matrix.length; row++)
for(int col = 0; col < matrix[row].length; col++)
if(row==col)
matrix[row][col] = Math.abs(matrix[row][col]);
}
```
• { {4, -5, 6},{-1, -2, 3} }
• How did the values in row1 change to those in row2 and vice versa? Why didn't any value change to the absolute value?
• { {4, 5, 6},{1, 2, 3} }
• How did the values in row1 change to those in row2 and vice versa?
• { {1, 2, 3},{4, 5, 6} }
• This would be true if all the matrix values were changed to their absolute value. But, this only happens when the row and column index are the same.
• { {-1, -2, 3},{4, -5, 6} }
• This would be true if none of the values in the matrix were changed. But, this will change the value to the absolute value when the row and column index are the same.
• { {1, -2, 3},{4, 5, 6} }
• This only changes the value in the matrix if the row and column index are the same. So this changes the values at (0,0) and (1,1).

15-4-6: What are the values of a and b after the for loop finishes?

```int a = 5, b = 2, temp;

for (int i=1; i<=4; i++)
{
temp = a;
a = i + b;
b = temp – i;
}
```
• a = 4 and b = 3
• This would be true if the for loop stopped when i was equal to 4.
• a = 7 and b = 0
• Here are the values of a and b at the end of each loop: i=1, a=3, b=4; i=2, a=6, b=3; i=3, a=4, b=3; i=4; a=7; b=0;
• a = 2 and b = -2
• Go back and check your values each time through the loop.
• a = 5 and b = 2
• This would be true if the loop stopped when i was equal to 6, but it stops when i is equal to 5.
• a = 9 and b = 2
• Keep a table of the variables and their values each time through the loop.

15-4-7: Condsider the following method. What value is returned from a call of mystery(4)?

```public static int mystery(int n)
{
if (n == 0)
return 1;
else
return 3 * mystery (n - 1);
}
```
• 243
• This would be true if it was mystery(5).
• 0
• How can this be? The value 0 is never returned.
• 3
• Did you notice the recursive call?
• 81
• This is the same as 3 to the 4th power (3 * 3 * 3 * 3 = 81).
• 27
• This would be true if it was mystery(3).

15-4-8: Which of the following correctly shows the iterations of an ascending (from left to right) insertion sort on an array with the following elements: {6,3,8,5,1}?

• {3,6,8,5,1}, {3,5,6,8,1}, {1,3,5,6,8}
• This is almost right, but there should be 4 of these steps.
• {1,3,8,5,6}, {1,3,8,5,6}, {1,3,5,8,6}, {1,3,5,6,8}
• This is selection sort, not insertion. Selection will find the smallest and swap it with the first element in the array.
• {3,6,8,5,1}, {3,6,8,5,1}, {3,5,6,8,1}, {1,3,5,6,8}
• An insertion sort will skip the first position and then loop inserting the next item into the correct place in the sorted elements to the left of the current item.
• {1,3,8,5,6}, {1,3,5,8,6}, {1,3,5,6,8}
• This is selection sort, not insertion and it is also an incorrect selection sort since it skips one step.
• {1,6,3,8,5}, {1,3,6,8,5}, {1,3,5,6,8}
• This doesn't match selection, insertion, or merge sort.

15-4-9: Consider the following code segment. How many times will a * be printed?

```for(int i = 0; i < 3; i++)
{
for(int j = 1; j <= 7; j++)
System.out.println("*");
}
```
• 21
• The general formula for the number times a loop executes is the last value - the first value + 1. The outer loop will execute 3 times (2-0+1) and the inner loop will execute 7 times (7-1+1) so the total is 3 * 7 = 21.
• 18
• This would be true if the inner loop stopped when j equals 7.
• 32
• This would be true if the outer loop executed 4 times and the inner loop 8, but is that right?
• 28
• This would be true if the outer loop executed 4 times, but is that right?
• 10
• This would be true if you added the number of times the outer loop executes and the number of times the inner loop executes, but you multiply them.

15-4-10: Consider the following method. What is the output from conditionTest(-3,2)?

```public static void conditionTest(int num1, int num2)
{
if ((num1 > 0) && (num2 > 0))
{
if (num1 > num2)
System.out.println("A");
else
System.out.println("B");
}
else if ((num2 < 0) && (num1 < 0)) {
System.out.println("C");
}
else if (num2 < 0) {
System.out.println("D");
}
else {
System.out.println("E");
}
}
```
• A
• This will only print if both num1 and num2 are greater than 0 and num1 is greater than num2.
• B
• This will only print if both num1 and num2 are greater than 0 and num1 is equal to or less than num2.
• C
• This will only print if both num1 and num2 are less than 0.
• D
• This will only print if num2 is less than 0 and num1 is greater than or equal to 0.
• E
• The first test will fail since num1 is less than 0, the second test will fail since num2 is greater than 0, the third test will also fail since num2 is greater than 0, which leads to the else being executed.

15-4-11: What is value of s1 after the code below executes?

```String s1 = "Hi There";
String s2 = s1;
String s3 = s2;
String s4 = s1;
s2 = s2.toUpperCase();
s3 = s3.toLowerCase();
s4 = null;
```
• hi there
• This would be true if we asked what the value of s3 was.
• HI THERE
• This would be true if we asked what the value of s2 was.
• Hi There
• Strings are immutable in Java which means they never change. Any method that looks like it changes a string returns a new string object. Since s1 was never changed to refer to a different string it stays the same.
• null
• This would be true if we asked what the value of s4 was.
• hI tHERE
• How could this have happened?

15-4-12: What is the output from the following code?

```String s = "Computer Science is fun!";
String s1 = s.substring(0,8);
String s2 = s1.substring(2);
String s3 = s2.substring(0,3);
System.out.println(s3);
```
• mp
• A substring of (0,3) will have 3 characters in it (index 0, index 1, and index 2).
• mpu
• Remember that substring with two numbers starts at the first index and ends before the second. So s1 = Computer, s2 = mputer, s3 = mpu
• mpur
• A substring of (0,3) will have 3 characters in it (index 0, index 1, and index 2).
• omp
• Remember that the first character in a string object is at index 0.
• om
• A substring of (0,3) will have 3 characters in it (index 0, index 1, and index 2).

15-4-13: Given the following class declarations, which declaration below will result in a compiler error?

```public class Book implements Comparable
{
// code for class
}

public class Dictionary extends Book
{
// code for class
}
```
• Book b = new Book();
• A object can always be declared to be of the type of the class that creates it.
• Dictionary d = new Book();
• The declared type must the the type of the class that creates the object or the type of any parent class. Dictionary is not a parent of the Book class.
• Comparable c = new Book();
• An object can be declared to be of an interface type if the interface type is one of the parent classes of the actual type.
• Book b = new Dictionary ();
• The declared type can be the actual type (the class that creates the object) or any parent of the actual type.
• Comparable c = new Dictionary();
• Since Dictionary inherits from Book and Book implements the Comparable interface, this is allowed.

15-4-14: What will the method below return when called with mystery(“xxzxyxx”)?

```public static int mystery(String str)
{
if (str.length() == 0) return 0;
else
{
if (str.substring(0,1).equals("x")) return 1 +
mystery(str.substring(1));
else return mystery(str.substring(1));
}
}
```
• 2
• This would be true if the recursion stopped when you first the first non "x", but is that what happens?
• 5
• This returns the number of "x"'s it finds in the str.
• 1
• Did you notice the recursive calls?
• 4
• How does it miss one "x"?
• 0
• Since the first character is "x" how can this be true?

15-4-15: Which will cause the longest execution of a binary search looking for a value in an array of 9 integers?

• The value is the first one in the array
• This could take a long time, but there is an answer that takes longer.
• The value is in the middle of the array
• This would be true if we were looking for the shortest execution of a binary search
• The value is at index 1 in the array
• This would be the second value checked if the value at the middle is greater than the desired value.
• The value isn’t in the array
• This will always take the longest when you are doing binary search.
• The value is at index 6 in the array
• This would be the second value checked if the value at the middle is less than the desired value.

15-4-16: Given the following array declaration and the fact that Animal is the parent class for Bird, Dog, Pig, Cat, and Cow, what is output from looping through this array of animals and asking each object to speak()?

```Animal[] a = { new Cat(), new Cow(), new Dog(), new Pig(), new Bird() }

Animal that has a method speak() which returns "Awk".
Bird doesn’t have a speak method
Dog has a speak method that returns “Woof”
Pig doesn’t have a speak method
Cow has a speak method that returns “Moo”
Cat has a speak method that returns "Meow"
```
• Awk Awk Awk Awk Awk
• This would be true if none of the children classes overrode the speak method, but many do.
• This won’t compile
• It is always okay to substitute a child object for a parent object.
• Meow Moo Woof Oink Tweet
• This would be true if Pig had a speak method that returned "Oink" and Bird had a speak method that returned "Tweet", but they do not. The inherited speak method will be called in Animal.
• Meow Moo Woof Oink Awk
• This would be true if Pig had a speak method that returned "Oink", but it does not.
• Meow Moo Woof Awk Awk
• Both Pig and Bird do not have a speak method so the one in Animal will be used.

15-4-17: What is the result of 17 (in base 16) - 13 (in base 8)?

• 4 in base 8
• You can't just subtract the two numbers since they are in different bases. Convert both to decimal first.
• 4 in base 16
• You can't just subtract the two numbers since they are in different bases. Convert both to decimal first.
• 00001100 in base 2
• 17 in base 16 is 23 in base 10. 13 in base 8 is 11 in base 10. The answer is 12 in base 10 which is 00001100 in base 2.
• 00000010 in base 2
• This is 2 in base 10. Convert both numbers to decimal and then convert the answer to binary.
• 4 in base 10
• You can't just subtract the two numbers since they are in different bases. Convert both to decimal first.

15-4-18: Consider the following method and code. What are the values of s and b after the following has executed?

```public static void test(int[] a, int y)
{
if (a.length > 1)
a = a * 2;
y = y * 2;
}

int[] s = {3,4};
int b = 4;
test(s,b);
```
• s={3, 8}; b=4;
• The value of a will be doubled since passing a copy of the value of s is a copy of the reference to the array. The value in b won't change since y will be set to a copy of b's value which is just a number.
• s={3, 4}; b=4;
• What about a = a * 2?
• s={6, 4}; b=4;
• Remember that the first index in an array is index 0. This code will double the second value in the array (the one at index 1).
• s={3, 8}; b=8;
• Java passes arguments by creating a copy of the current value so the value of b won't be affected by changes to y.
• s={6, 8}; b=8;
• Java passes arguments by creating a copy of the current value so the value of b won't be affected by changes to y.

15-4-19: Which of the following is (are) true?

```I.  Insertion sort takes longer when the array is sorted in ascending order and
you want it sorted in descending order.
II.  Mergesort uses recursion.
III.  Selection sort takes less time to execute if the array is already sorted
in the correct order.
```
• I only
• This is true, but at least one other thing is true as well.
• II only
• This is true, but at least one other thing is true as well.
• III only
• Selection sort always takes the same amount of time to execute.
• I and II only
• Mergesort does use recursion (has a method that calls itself). Insertion sort does take longer to execute when the items to be sorted are in ascending order and you want them in descending order.
• I, II, and III
• Selection sort always takes the same amount of time to execute.

15-4-20: Given the following code, how many calls to mystery are made (including the first call) when mystery(0, 4, 5) is executed when arr = {1, 2, 3, 5, 7}?

```private int[] arr;

public int mystery(int low, int high, int num) {

int mid = (low+high) / 2;

if (low > high) {
return -1;
}
else if (arr[mid] < num) {
return mystery(mid +1, high, num);
}
else if (arr[mid] > num) {
return mystery(low, mid - 1, num);
}
else
return mid;
}
```
• The method is recursive and the first call it will compare 3 to 5 and then do mystery(3,4,5).
• 1
• There are two calls: mystery(0, 4, 5) and mystery(3, 4, 5).
• 2
• This would be true if it was mystery(0, 4, 7);
• 3
• This would be true if we were looking for a number that isn't in the array.
• 4
• At most this will take log base 2 of the size of the array plus one to determine that the desired value isn't in the array.
• 5