# 13.3. Practice Exam 3 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.

- 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.
- 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?
- 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?
- { {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).
- 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.
- 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).
- {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.
- 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.
- 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.
- 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?
- 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).
- 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.
- 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.
- 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?
- 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.
- 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.
- 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.
- s={3, 8}; b=4;
- The value of a[1] 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[1] = a[1] * 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.
- 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.
- 1
- No, the method is recursive and in the first call it will compare 3 to 5 and then do mystery(3,4,5).
- 2
- There are two calls: mystery(0, 4, 5) and mystery(3, 4, 5).
- 3
- This would be true if it was mystery(0, 4, 7);
- 4
- This would be true if we were looking for a number that isn't in the array.
- 5
- 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.

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

```
1public class C1 {
2 private int num;
3 private String name;
4
5 public C1(int theNum) {
6 num = theNum;
7 }
8
9 public C1(String theName) {
10 name = theName;
11 }
12 // other methods not shown
13}
14
15public class C2 extends C1 {
16// methods not shown
17}
18
19Possible constructors
20I. public C2 () { }
21II. public C2 (int quan) {super (quan); }
22III. public C2 (String label) { super(label); }
```

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

13-3-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?

```
1public static void sort(int[] a) {
2 int maxCompare = a.length - 1;
3 int savedIndex = 0;
4 int numSteps = 0;
5 int temp = 0;
6
7 for (int i = maxCompare; i > 0; i--) {
8 savedIndex = i;
9
10 for (int j = i - 1; j >= 0; j--) {
11 /* missing code */
12 }
13
14 temp = a[i];
15 a[i] = a[savedIndex];
16 a[savedIndex] = temp;
17 }
18}
```

13-3-4: 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?

```
1int[][] matrix = new int[2][3];
2
3public static void changeMatrix(int[][] matrix )
4{
5 for (int row = 0; row < matrix.length; row++)
6 for(int col = 0; col < matrix[row].length; col++)
7 if(row==col)
8 matrix[row][col] = Math.abs(matrix[row][col]);
9}
```

13-3-5: What are the values of a and b after the for loop finishes?

```
1int a = 5, b = 2, temp;
2
3for (int i=1; i<=4; i++)
4{
5 temp = a;
6 a = i + b;
7 b = temp – i;
8}
```

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

```
1public static int mystery(int n)
2{
3 if (n == 0)
4 return 1;
5 else
6 return 3 * mystery (n - 1);
7}
```

13-3-7: 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}?

13-3-8: Consider the following code segment. How many times will a * be printed?

```
1for(int i = 0; i < 3; i++)
2{
3 for(int j = 1; j <= 7; j++)
4 System.out.println("*");
5}
```

13-3-9: Consider the following method. What is the output from conditionTest(-3,2)?

```
1public static void conditionTest(int num1, int num2)
2{
3 if ((num1 > 0) && (num2 > 0))
4 {
5 if (num1 > num2)
6 System.out.println("A");
7 else
8 System.out.println("B");
9 }
10 else if ((num2 < 0) && (num1 < 0)) {
11 System.out.println("C");
12 }
13 else if (num2 < 0) {
14 System.out.println("D");
15 }
16 else {
17 System.out.println("E");
18 }
19}
```

13-3-10: What is value of s1 after the code below executes?

```
1String s1 = "Hi There";
2String s2 = s1;
3String s3 = s2;
4String s4 = s1;
5s2 = s2.toUpperCase();
6s3 = s3.toLowerCase();
7s4 = null;
```

13-3-11: What is the output from the following code?

```
1String s = "Computer Science is fun!";
2String s1 = s.substring(0,8);
3String s2 = s1.substring(2);
4String s3 = s2.substring(0,3);
5System.out.println(s3);
```

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

```
1public class Book
2{
3 // code for class
4}
5
6public class Dictionary extends Book
7{
8 // code for class
9}
```

13-3-13: What will the method below return when called with mystery(“xxzxyxx”)?

```
1public static int mystery(String str)
2{
3 if (str.length() == 0) return 0;
4 else
5 {
6 if (str.substring(0,1).equals("x")) return 1 +
7 mystery(str.substring(1));
8 else return mystery(str.substring(1));
9 }
10}
```

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

13-3-15: 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()?

```
1Animal[] a = { new Cat(), new Cow(), new Dog(), new Pig(), new Bird() }
2
3Animal that has a method speak() which returns "Awk".
4Bird doesn’t have a speak method
5Dog has a speak method that returns “Woof”
6Pig doesn’t have a speak method
7Cow has a speak method that returns “Moo”
8Cat has a speak method that returns "Meow"
```

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

13-3-17: Consider the following method and code. What are the values of s and b after the following has executed?

```
1public static void test(int[] a, int y)
2{
3 if (a.length > 1)
4 a[1] = a[1] * 2;
5 y = y * 2;
6}
7
8int[] s = {3,4};
9int b = 4;
10test(s,b);
```

13-3-18: Which of the following is (are) true?

```
1I. Insertion sort takes longer when the array is sorted in ascending order and
2 you want it sorted in descending order.
3II. Mergesort uses recursion.
4III. Selection sort takes less time to execute if the array is already sorted
5 in the correct order.
```

13-3-19: 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}?

```
1private int[] arr;
2
3public int mystery(int low, int high, int num) {
4
5 int mid = (low+high) / 2;
6
7 if (low > high) {
8 return -1;
9 }
10 else if (arr[mid] < num) {
11 return mystery(mid +1, high, num);
12 }
13 else if (arr[mid] > num) {
14 return mystery(low, mid - 1, num);
15 }
16 else
17 return mid;
18}
```