13.2. Practice Exam 2 for the AP CSA Exam¶
The following 20 questions are similar to what you might see on the AP CSA 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.
- 10
- This would be true if it was adding up all the values in the third column, the one at index 2.
- 15
- This would be true if it was adding up all the values in the first column, the one at index 0.
- 17
- This code adds up all the values in the second column, the one at index 1 since column indices start at 0.
- 12
- This would be true if it was adding up all the values in the last column, the one at index 3.
- 22
- This would be true if it was adding up all the values in the first row, but the row changes each time through the for loop.
- [9, 3, 17, 2, 16, 4, 1]
- This would be true if it moved all of the values from the front to the back. But does it?
- [1, 4, 16, 2, 17, 3, 9]
- This would be true if the code reversed the list. But does it? Remember that remove(0) removes the first item in the list and returns it. The add method adds the item to the end of the list.
- [9, 3, 17, 16, 4, 1, 2]
- This would be true if only the value 2 was moved to the end of the list. Is that what this code does?
- [16, 4, 1, 9, 3, 17, 2]
- This code moves the first item to the end of the list 4 times. So it moves the 9, 3, 17, and 2.
- [2, 16, 4, 1, 9, 3, 17]
- This would be true if the call was mystery(3) instead of mystery(4). Then it would move the first 3 values in the list to the end of the list.
- Interface
- An interface is a special kind of abstract class. It isn't a type of relationship between classes.
- Polymorphism
- Polymorphism is using the run-time type of the object to determine which method to run. It isn't a type of relationship between classes.
- Inheritance (is-a)
- Inheritance is when one class (the child class) extends the other (the parent class). Do you see the keyword extends here?
- Association (has-a)
- Association is when one class keeps track of one or more objects of the other class. In this case a DogOwner object has an array of dog objects.
- Overloading
- Overloading is when a class has two methods with the same name but the parameter lists are different. It is not a type of relationship between classes.
- return 9 * y;
- The first line is the same as 3y. Then you have to substitute in the new value of y in the second line (2 * 3y + 3y) = 6y + 3y = 9y.
- return 7 * y;
- Remember that the second line is using the value of y calculated in the first line for both places y appears.
- return y;
- This would be true if the body only had the last line in it. What do the first 2 lines do?
- return 3 * y;
- This would be true if the it was missing the second line. What does that line do?
- return 4 * y;
- Remember that 2 * y + y is the same as 3 * y.
- test();
- This would run the test method in class C since the object was created by the C class. When a method is called the runtime system will start looking for the method in the class that created the object.
- super.super.test();
- You can't use super.super. This would cause a compile-time error.
- super.test();
- This would run the test method in class B since super is used to run a method in your parent class and B is the parent of C.
- this.test();
- This would run the test method in class C.
- There is no way to call a method in a grandparent class from a grandchild class
- You can use super to force the runtime to run a method in a parent class, but there is no way to force a call to a method in a grandparent (parent of your parent) class.
- v.test(sporty,v);
- This would be true if the test method took a SportsCar object and a Vehicle object.
- sporty.test(c,c);
- This would be true if the test method took two Car objects or a Car and a Vehicle object.
- v.test(sporty,c);
- This would be true if the test method took a SportsCar object and a Car object.
- sporty.test(sporty,v);
- This would be true if the test method took a SportsCar object and a Vehicle object.
- c.test(sporty,sporty);
- The test method takes a Car object and a SportsCar object. Only this answer correctly passes a SportsCar object as the second parameter. You can use a SportsCar object as a Car object since it is a subclass of Car. The test method can be called on any child of Vehicle.
- 8
- The call recur(5) will return recur(4) + recur(3). The call recur(4) returns 5. The call recur(3) returns 3. So recur(5) returns 5 + 3 = 8.
- 1
- This method will only return 1 when n is less than or equal to 1. In this case n is 5.
- 2
- This would be true if the call was recur(2). This would return recur(1) + recur(0). Both recur(1) and recur(0) would return 1 so recur(2) would return 1 + 1 = 2.
- 5
- This would be true if the call was recur(4). This would return recur(3) + recur(2). The call recur(3) returns 3. The call recur(2) returns 2. So recur(4) returns 3 + 2 = 5.
- 3
- This would be true if the call was recur(3). This would return recur(2) + recur(1). The call to recur(1) would return 1. The call to recur(2) would return recur(1) + recur(0). Both recur(1) and recur(0) would return 1 so recur(2) would return 1 + 1 = 2. Thus recur(3) would return 2 + 1 = 3.
- 1 3 5 7 9 11 13 15 17 19
- This would be true if k was printed when the reminder was equal to 1 (when the value was odd).
- 0 2 4 6 8 10 12 14 16 18
- This code will loop through all the values from 0 to 19, but only print the ones that are even (dividing by 2 has a remainder of 0).
- 2 4 6 8 10 12 14 16 18
- The first time through the loop k will have the value 0 and 0 % 2 returns 0 so the 0 will print.
- 3 6 9 12 15 18
- This would be true if the test was (k % 3 == 0) and the loop started with k = 1.
- 0 2 4 6 8 10 13 14 16 18 20
- The loop will stop when k has the value of 20. So it won't print a 20.
- [a, c, e, d, g]
- This would be true if it was list.add(2, "e") instead of list.set(2, "e").
- [c, e, d, b, g]
- This would be true if the first item in a list was at index 1, but it is at index 0.
- [a, c, e, g]
- This code adds "a" to the end of the list: ["a"] and then "b" to the end of the list: ["a", "b"]. Then it changes the value at index 1 to "c": ["a", "c"]. Then it adds "d" at position 2 which first moves to the right any existing values ["a", "c", "d"]. Then it sets the value at index 2 to "e": ["a", "c", "e"]. Then it adds "g" to the end: ["a", "c", "e", "g"].
- [a, b, e, d, g]
- For this to be true the 3rd line would have to be list.add("c"). Is it?
- [a, c, e, d, b, g]
- This would be true if all of the sets were adds.
- pm1pm2cm2cm1
- When p.m1() is run it will execute the m1 method in Child since p is an object of that class. The first line calls super.m1() which will execute the m1 method in Parent. That method will print "pm1" and then call m2(). The m2 method in child will execute since p is a Child object. The first line in that method calls super.m2() which will execute the m2 method in Parent. This will print "pm2". Then the parent m2 method will return, so execution will continue in the m2 method of Child and it will print "cm2". Then the child m2 method will return which will continue execution in the m1 method of Child which will print "cm1".
- pm1pm2
- This would be true if p was an object of the Parent class, but it is an object of the Child class and the runtime will start execution of a method in the Child class method if it has it.
- pm1pm2cm1cm2
- Remember that each method call is added to the call stack and after the method returns execution continues with the next statement after the method call.
- pm1cm1
- This would be true if the m1 method in Parent didn't call m2().
- pm1
- This would be true if the m1 method in Parent didn't call m2() and the p was actually an object of the Parent class.
- I only
- I does work, but so does another one.
- II only
- The compiler will look for the method based on the declared type. The declared type for b is Animal and Animal doesn't have a growl method.
- III only
- III does work, but so does another one.
- I and III only
- I works since the declared type is Animal and Animal has an eat method. III works because the cast tells the compiler to treat b is a Bear and Bear has a growl method.
- I, II, and III
- Does Animal have a growl method? Remember that the compiler checks for the method using the declared type.
- Mirrors the values from the top half to the bottom half of the 2D array
- This would be true if it was p[height - row - 1][col] = p[row][col];
- Mirrors the values from the left halt to the right half of the 2D array
- This would be true if it was looping through all the rows and half the columns and copying from p[row][width - col - 1] = p[row][col];
- Mirrors the values from the bottom half to the top half of the 2D array
- This loops through the top half rows (height / 2) and mirrors the values from the bottom half p[row][col] = p[height - row - 1][col]; So p[0][0] = p[height - 1][0] and p[0][1] = p[height - 1][1].
- Mirrors the values from the right half to the left half of the 2D array
- This would be true if it was looping through all the rows and half the columns and copying from p[row][width - col - 1] = p[row][col];
- All values remain the same.
- How can this be true since p[row][col] = p[height - row - 1][col]?
- 12344321
- This method prints the right most digit (x % 10 returns the right most digit) and then if x / 10 is not equal to 0 (x < 10) it returns mystery of the current number after chopping off the right most digit. So mystery(4321) prints 1 and then calls mystery(432) which prints 2 and then calls mystery(43) which prints 3 and then calls mystery (4) which prints 4. Since 4 / 10 is equal to 0 it won't do a recursive call. It prints 4 again and mystery(4) returns. Execution will return to mystery(43) after the recursive call to mystery(4) and the 3 will print and then mystery (43) will return. Execution will return to mystery(432) after the recursive call to mystery(43) and the 2 will print and then mystery (432) will return. Execution will return to mystery(4321) after the recursive call to mystery(432) and the 1 will print and then mystery (4321) will return.
- 1234
- This would be true if there wasn't a second System.out.print(x % 10) after if.
- 4321
- This would be true if the first call to System.out.print(x % 10); wasn't in the method.
- 43211234
- This would be true if it was mystery(1234).
- 32144123
- How does the 3 get printed first? Remember that x % 10 returns the right most digit in x.
- public class Room extends Classroom implements Building { … }
- Is a Classroom a type of Building? Don't use extends unless an object of the child class can be substituted for a object of the parent class.
- public class Classroom extends Room { … } public class Building { private Room[] rooms; …. }
- If a classroom is a room, then Classroom should extend Room (inherit from it). If a Building has rooms it should have a field that holds them. Since a Building can have more than one Room we can use an array to hold the rooms.
- public class Room extends Building { private Classroom room; …. }
- Is a Room a type of Building? Don't use extends unless the child is the same type of thing as the parent.
- public class Classroom extends Building, Room { … }
- You can't extend two classes in Java so this can't be right.
- public class Room extends Classroom, Building { … }
- You can't extend two classes in Java so this can't be right.
- Whenever the first element in a is equal to val
- What is count for?
- Whenever a contains any element which equals val.
- It only sets temp to true when count is greater than 1.
- Whenever more than 1 element in a is equal to val.
- This increments count once for each value in a that is equal to val. It returns true when count is greater than 1.
- Whenever exactly 1 element in a is equal to val.
- This would be true if it was temp = count == 1.
- Whenever the last element in a is equal to val.
- This could be one line of code return (a[a.length-1] == val).
- 1 1 1 2 2 1 2 2 3 1 3 2 4 1 4 2 5 1 5 2
- This would be true if line 3 was: System.out.print(j + " " + k + " ");
- 1 2 2 4 3 6 4 8
- This would be true if line 1 was: for (int j = 1; j < 5; j++).
- 1 1 1 2 2 1 2 2 3 1 3 2 4 1 4 2
- This would be true if line 1 was: for (int j = 1; j < 5; j++) and if line 3 was: System.out.print(j + " " + k + " ");
- 5 10 15 4 8 12 3 6 9 2 4 6 1 2 3
- This would be true if line 1 was: for (int j = 5; j >=1; j--) and line 2 was: for (int k = 1; k <= 3; k++).
- 1 2 2 4 3 6 4 8 5 10
- This prints j * k and for each value of j from 1 to 5, k changes from 1 to 2. So when j is 1 it will print 1 2. When j is 2 it will print 2 4. When j is 3 it will print 3 6. When j is 4 it will print 4 8. When j is 5 it will print 5 10.
- method1 and method3
- The problem with method2 is that if num1==num2 the first if will execute, but so will the second if and result will be set to 1.
- method1 only
- Another will work as well.
- method2 only
- This one won't work. The problem with method2 is that if num1==num2 the first if will execute, but so will the second if and result will be set to 1.
- method2 and method3
- While method3 will work, method2 won't. The problem with method2 is that if num1==num2 the first if will execute, but so will the second if and result will be set to 1.
- all of them
- Two will work, but one will not.
- 13, 28
- It will print the value of t before changing it, so it will print 13 first and the loop ends when t is equal to 29 so the last time it will print 28.
- 13, 29
- It prints the value of t before changing it and the loop ends when t equals 29 so how can this be true?
- 14, 28
- It prints the value of t before changing it and t starts at 13 so it will print 13 first.
- 14, 29
- It prints the value of t before changing it, so neither of these is correct.
- 1, 28
- How could it print 1 for the value of t when t is set to 13 initially?
- I and III
- These are both true, but one more is also true.
- All are true
- Since s1 and s2 were created using the new operator they do not refer to the same object so s1 == s2 is false.
- I, III, and IV
- I is true since they have the same characters in the same order. III and IV are both true since they refer to the same object.
- II and IV
- II is not true. Since s1 and s2 were created using the new operator they do not refer to the same object so s1 == s2 is false.
- III and IV
- These are both true, but one more is also true.
13-2-1: What is the value of total after the following code executes?
int[][] matrix = { {4, 5, 6, 7}, {0, 1, 2, 3}, {3, 2, 1, 0}, {8, 9, 1, 2}};
int total = 0;
for (int row = 0; row < matrix.length; row++)
{
total = total + matrix[row][1];
}
13-2-2: Assume that list has been initialized with the following Integer objects: [9, 3, 17, 2, 16, 4, 1]. Which of the following shows the values in list after a call of mystery(4)?
private List list<Integer>;
public void mystery(int n)
{
for (int i= 0; i < n; i++)
{
Object obj = list.remove(0);
list.add((Integer)obj);
}
}
13-2-3: Given the following two class declarations, what is the relationship between Dog and DogOwner?
public class Dog
{
private String name;
public void setName(String n)
{
name = n;
}
public String getName()
{
return name;
}
}
public class DogOwner
{
private String name;
private Dog[] dogs;
}
13-2-4: Which of the following expressions can be use to replace the body of mystery so that mystery will return the same result for all values of y?
public static int mystery(int y)
{
y = 2 * y + y;
y = 2 * y + y;
return y;
}
13-2-5: Class C extends class B, which extends class A. Also, all of the three classes implement a public method test(). How can a method in an object of class C invoke the test() method defined in class A (without creating a new instance of class A)?
13-2-6: Which of the following is a correct call to the test
method?
public class Vehicle
{
public void test(Car x, SportsCar y) {}
}
public class Car extends Vehicle {}
public class SportsCar extends Car {}
public class VehicleTest
{
public static void main(String[] args)
{
Vehicle v = new Vehicle();
Car c = new Car();
SportsCar sporty = new SportsCar();
}
}
13-2-7: Given the following method declaration. What value is returned from recur(5)?
public static int recur(int n)
{
if (n <= 1)
return 1;
else
return (recur(n-1) + recur(n-2));
}
13-2-8: What is printed when the following code is run?
for (int k = 0; k < 20; k = k + 1)
{
if (k % 2 == 0)
System.out.print(k + " ");
}
13-2-9: What is printed when the following code executes (runs)?
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.set(1,"c");
list.add(2, "d");
list.set(2, "e");
list.add("g");
System.out.println(list);
13-2-10: Assume that Parent p = new Child(); appears in a client program. What is the result of the call p.m1()?
public class Parent
{
public void m1()
{
System.out.print("pm1");
m2();
}
public void m2()
{
System.out.print("pm2");
}
}
public class Child extends Parent
{
public void m1()
{
super.m1();
System.out.print("cm1");
}
public void m2()
{
super.m2();
System.out.print("cm2");
}
}
13-2-11: Assume the following classes.
public class Animal
{
// constructors not shown
public void eat()
{ // code not shown
}
}
public class Bear extends Animal
{
// constructors not shown
public void growl()
{ // code not shown
}
}
Assume that the following declaration is in a different class.
Animal b = new Bear();
Which of the following will compile without error?
I. b.eat();
II. b.growl;
III. ((Bear) b).growl();
13-2-12: Which of the following best explains what the method m does?
public void m(int[][]p)
{
int height = p.length;
for (int row = 0; row < height / 2; row++)
{
for (int col = 0; col <p[0].length; col++)
{
p[row][col] = p[height - row - 1][col];
}
}
}
13-2-13: What is the output from mystery(4321) when mystery is defined as follows?
//precondition: x >=0
public static void mystery (int x)
{
System.out.print(x % 10);
if ((x / 10) != 0)
{
mystery(x / 10);
}
System.out.print(x % 10);
}
13-2-14: A classroom is a room and a building has many rooms. If the three classes Room, Classroom, and Building create objects that have the same relationship which of the following is the most appropriate set of declarations?
13-2-15: Given the following code which of the answers best describes the conditions needed for temp to be true when it is returned?
boolean temp = false;
int count = 0;
for ( int testVal : a)
{
if ( testVal == val )
count++;
}
temp = count > 1;
return temp;
13-2-16: What is the output from the following code segment?
for (int j = 1; j <=5; j++)
{
for (int k = 1; k < 3; k++)
System.out.print(j * k + " ");
}
13-2-17: Consider the following methods. Which of method1, method2, and method3 would give the same result as sample?
public void sample(int num1, int num2)
{
int result = 99;
if (num1==num2)
{
result = 0;
}
else if (num1>num2)
{
result = 1;
}
else {
result = -1;
}
System.out.println(result);
}
public void method1(int num1, int num2)
{
int result=99;
if (num1 == num2)
{
result = 0;
}
else {
if(num1 > num2)
{
result = 1;
}
else
{
result = -1;
}
}
System.out.println(result);
}
public void method2(int num1, int num2)
{
int result = 99;
if (num1 == num2)
{
result = 0;
}
if (num1 >= num2)
{
result = 1;
}
else
{
result = -1;
}
System.out.println(result);
}
public void method3(int num1, int num2)
{
int result = 99 ;
if (num1 == num2)
{
result = 0;
}
if (num1 > num2)
{
result = 1;
}
if (num1 < num2)
{
result = -1;
}
System.out.println(result);
}
13-2-18: What are the first and last values output by the following code segment?
int t = 13;
while (t < 29)
{
System.out.println(t);
t++;
}
13-2-19: Given the following code.
String s1 = new String("hi");
String s2 = new String("hi");
String s3 = s2;
Which of the following would return true:
I. s1.equals(s2)
II. s1 == s2
III. s2.equals(s3);
IV. s2 == s3;