This book is now obsolete Please use CSAwesome instead.
15.5. Practice Exam 1 for the first half of the AP CS A Curriculum¶
The following 20 questions are similar to what you might see assessing the first half of the AP CS A coursework on the AP 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.
- 10001111
- Correct!
- 10011011
- This is the binary representation of 155.
- 10111010
- This is the binary representation of 186.
- 10101101
- This is the binary representation of 173.
- 10110001
- This is the binary representation of 177.
- 0 3 6 9 12 15
- This would be true if this loop had the correct syntax on the print.
- 0 1 2 3 4 5
- The conditional would only match multiples of three, and the program does not output anything due to incorrect syntax.
- 1 4 7 10 13
- The conditional would only match multiples of three, and the program does not output anything due to incorrect syntax.
- This code will not print anything.
- The syntax to output text is System.out.print(), so this code will cause a compiler error.
- 0 3 6 9 12
- The program would also output 15, since the loop runs until x <= 15. However, the program does not output anything due to incorrect syntax.
- I only
- This implementation of ''addMinutes'' does not account for values of additionMinutes that push the minute count above 60.
- II only
- Implementation II works, but implementation III also works.
- IV only
- Implementation IV does not work for situations where additionMinutes + minutes does not go above 60.
- II and III
- Correct!
- I, II, and III
- Implementations II and III are correct, but implementation I is not. Implementation I does not account for values of additionMinutes that push the minute account above 60.
- int[] nums = [5];
- The left side is okay, but the right side is wrong.
- int nums[5];
- This correctly declares an array of five integers.
- int[] nums = { 2, 4, 6, 8, 10 };
- This correctly declares and initializes an array of five integers.
- int[] nums; nums = new int[5];
- This declares nums to be an array of integers and then creates the array.
- numStudents
- numStudents is brief, but still tells the user what the variable represents. Num is a common shortening of number and is allowed.
- numberOfStudentsInClass
- While this name gives the user information about what the variable represents, it is too long and therefore should be shorter.
- numberStudents
- Number is commonly abbreviated as num and should be abbreviated here.
- numStud
- Num is a satisfactory abbreviation of number, but stud is not a common enough abbreviation for student.
- #students
- Variable names can start with '_', '$', or a letter, but not '#'.
- class
- Class is a reserved keyword. It is used to declare a class.
- boolean
- Boolean is a reserved keyword and a primitive data type.
- String
- Correct! All other options are reserved keywords in Java.
- static
- Static is a reserved keyword in Java.
- void
- Void is a reserved keyword in Java.
- { 1, 2, 5, 4, 3 }
- Correct!
- { 1, 2, 5, 4, 5 }
- Incorrect. Remember that arrays are indexed from 0.
- { 5, 4, 1, 2, 3 }
- Incorrect, temp is used to hold the value from index 2 and that value is put in index 4.
- { 1, 2, 4, 4, 3 }
- Nums at index 2 is set to the value of nums at index 4, not just the value 4.
- { 1, 4, 3, 2, 5 }
- Incorrect. Remember that arrays are indexed from 0.
- !(a && b)
- This would be true in any case where a and b weren't both true
- !a && b
- If b was false, this option would be false.
- !a && !b
- Correct!
- a && b
- This will only be true only when both a and b are true.
- a || !b
- This will only be true if a is true, or b is false.
- B3DD
- This is 1011001111011101 in hexadecimal.
- AC32
- This is 1010110000110010 in hexadecimal.
- B6D4
- Correct!
- B6D9
- This is 1011011011011001 in hexadecimal.
- A8E2
- This is 1010100011100010 in hexadecimal.
- Prints the string in reverse order
- This method prints the reversed string.
- Deletes the second half of the string
- Incorrect, this method prints the parameter reversed.
- Prints string normally
- Incorrect, this method prints the parameter reversed.
- Compile-time error occurs
- Incorrect, this method prints the parameter reversed.
- Prints alternating characters from beginning and end of the string.
- Incorrect, this method prints the parameter reversed.
- "Hello World!"
- The variable holds all characters that were stored at even indices for the original phrase.
- "Hello "
- The variable holds all characters that were stored at even indices for the original phrase.
- "He"
- The variable holds all characters that were stored at even indices for the original phrase.
- "HloWrd"
- Correct! The variable holds all characters that were stored at even indices for the original phrase.
- "el ol!"
- The variable holds all characters that were stored at even indices for the original phrase.
- The value is the first one in the array
- This would be true for the shortest execution. This would only take one execution of the loop.
- The value is in the middle of the array
- This would take 5 executions of the loop.
- The value is at position 3 in the array
- This would take 3 executions of the loop.
- The value isn't in the array
- A sequential search loops through the elements of an array starting with the first and ending with the last and returns from the loop as soon as it finds the passed value. It has to check every value in the array when the value it is looking for is not in the array. This would take 10 executions of the loop.
- The value is at position 6 in the array
- This would take 6 executions of the loop.
- IV
- All of these are valid reasons to use an inheritance hierarchy.
- V
- In fact, all of the reasons listed are valid. Subclasses can reuse methods written for superclasses without code replication, subclasses can be stored in the same array, and passed as arguments to methods meant for the superclass. All of which make writing code more streamlined.
- I and II
- III is also valid. In some cases you might want to store subclasses together in a single array, and inheritance allows for this.
- I and III
- II is also valid. In some cases a single method is applicable for a number of subclasses, and inheritance allows you to pass objects of the subclasses to the same method instead of writing individual methods for each subclass.
- I only
- II and III are also valid, in some cases a single method is applicable for a number of subclasses, and inheritance allows you to pass all the subclasses to the same method instead of writing individual methods for each subclass and you might want to store subclasses together in a single array, and inheritance allows for this.
- 4
- This would be true if it was
return(a[1]*= 2);
. - 16
- This would be true if the return statement was
return (a[0]*=2);
. - 7
- This would be true if it was
a[0]--;
Or it would be true if array indicies started at 1, but they start with 0. - 2
- The statement
a[1]--;
is the same asa[1] = a[1] - 1;
so this will change the 3 to a 2. The return(a[1] * 2)
does not change the value ata[1]
. - 3
- This can't be true because
a[1]--;
means the same asa[1] = a[1] - 1;
So the 3 will become a 2. Parameters are all pass by value in Java which means that a copy of the value is passed to a method. But, since an array is an object a copy of the value is a copy of the reference to the object. So changes to objects in methods are permanent. - a = 6 and b = 7
- This would be true if the loop stopped when i was equal to 6.
- a = 6 and b = 13
- Actually i = 6 and t = 6 and a = 13 after the loop finishes.
- a = 13 and b = 0
- The variable i loops from 1 to 6
i = 1, t = 10, a = 4, b = 9
i = 2, t = 4, a = 11, b =2
i = 3, t = 11, a = 5, b = 8
i = 4, t = 5, a = 12, b = 1
i = 5, t = 12, a = 6, b = 7
i = 6, t = 6, a = 13, b = 0 - a = 6 and b = 0
- Actually i = 6 and t = 6 and b = 0 after the loop finishes.
- a = 0 and b = 13
- No a = 13 and b = 0 after the loop finishes.
- hi there
- This would only be correct if we had s1 = s2; after s2.toLowerCase(); was executed. Strings are immutable and so any change to a string returns a new string.
- HI THERE
- This would be correct if we had s1 = s3; after s3.toUpperCase(); was executed. Strings are immutable and so any change to a string returns a new string.
- Hi There
- Strings are immutable meaning that any changes to a string creates and returns a new string, so the string referred to by s1 does not change
- null
- This would be true if we had s1 = s4; after s4 = null; was executed. Strings are immutable and so any changes to a string returns a new string.
- hI tHERE
- Strings are immutable and so any changes to a string returns a new string.
- 4
- This would be correct if the variable col was 0 because then it would add 1 + 1 + 1 + 1 which is 4.
- 8
- The variable col is 2, so it adds 2 + 2 + 3 + 1 which is 8.
- 9
- This would be correct if the variable col was 1 because then it would add 1 + 2 + 2 + 4 which is 9.
- 12
- This would be correct if the variable col was 3 becuase then it would add 2 + 4 + 4 + 2 which is 12.
- 10
- This would be true if we were adding the values in the 3rd row (row = 2) instead of the 3rd column. This would be 1 + 2 + 3 + 4 which is 10.
- { { 2, 1, 1, 1 }, { 2, 2, 1, 1 }, { 2, 2, 2, 1 } }
- This would be true if it was filling mat with 1 if the row index is less than the column index, but it fills with a 3 in this case.
- { { 2, 3, 3, 3 }, { 1, 2, 3, 3 }, { 1, 1, 2, 3 } }
- This will fill mat with 3 if the row index is less than the column index, 2 if the row index is equal to the column index, and a 1 if the row index is greater than the column index.
- { { 2, 1, 1 }, { 2, 2, 1 }, { 2, 2, 2 }, { 2, 2, 2 } }
- This would be true if it was int [][] mat = new int [4][3] and it filled the mat with 1 if the row index is less than the column index.
- { { 2, 3, 3 }, { 1, 2, 3 }, { 1, 1, 2 }, { 1, 1, 1 } }
- This would be true if it was int [][] mat = new int [4][3]. Remember that the first number is the number of rows.
- { { 1, 3, 3, 3 }, { 2, 1, 3, 3 }, { 2, 2, 1, 3 } }
- This would be true if it filled the mat with 1 if the row and column indices are equal and 2 if the row index is greater than the column index.
- The values don't matter, this will always cause an infinite loop.
- An infinite loop will not always occur in this program segment.
- Whenever a has a value larger than temp.
- Values larger then temp will not cause an infinite loop.
- When all values in a are larger than temp.
- Values larger then temp will not cause an infinite loop.
- Whenever a includes a value equal to temp.
- Values equal to temp will not cause an infinite loop.
- Whenever a includes a value that is less than or equal to zero.
- When a contains a value that is less than or equal to zero, then multiplying that value by 2 will never make the result larger than the temp value (which was set to some value > 0), so an infinite loop will occur.
- A
- This would be true if num1 and num2 were both greater than 0 and num1 was greater than num2. However, num2 is less than 0.
- B
- This would be true if num1 and num2 were both greater than 0 and num1 was less than or equal to num2. However, num2 is less than 0.
- C
- The first test is false since num2 is less than 0 and for a complex conditional joined with And (&&) to be true both expressions must be true. Next,
else if ((num2<0) || (num1<0))
is executed and this will be true since num2 is less than 0 and for a complex conditional joined with Or (||) only one of the expressions must be true for it to execute. - D
- This will not happen since if num2 is less than 0 the previous conditional would be true
((num2<0) || (num1<0))
- E
- This will not happen since if num2 is less than 0 the previous conditional would be true
((num2<0) || (num1<0))
15-5-1: What is the value of 143 (base-10) in binary(base-2)?
15-5-2: What will be printed after this code is executed?
for (int i = 0; i <= 15; i++) {
if (i % 3 == 0) {
System.print(i + " ");
}
}
15-5-3: Consider the following declaration for a class that will be used to represent points in time. Which of these options correctly implement ‘’addMinutes()’’?
public class Timer
{
private int hours; // number of hours
private int minutes; // 0 <= minutes < 60
void addHours(int addition)
{
hours = hours + addition;
}
void addMinutes(int additionMinutes)
{
// implementation not shown
}
// ... other methods not shown
}
Proposed Implementations:
I. public void addMinutes(int additionMinutes)
{
minutes = minutes + additionMinutes;
}
II. public void addMinutes(int additionMinutes)
{
if(minutes + additionMinutes >= 60)
{
hours += 1;
minutes -= 60;
}
else
{
minutes += minutes + additionMinutes;
}
}
III. public void addMinutes(int additionMinutes)
{
minutes += additionMinutes;
if(minutes >= 60)
{
hours++;
minutes -= 60;
}
}
IV. public void addMinutes(int additionMinutes){
{
if(additionMinutes + minutes >= 60)
{
minutes = additionMinutes + minutes - 60;
hours += 1;
}
}
15-5-4: Which is NOT a correct way to declare an array of 5 integers?
15-5-5: Which of the following is a good name for an integer variable that represents the number of students in a class?
15-5-6: Which of the following is NOT a reserved keyword in Java
15-5-7: What are the contents of nums after the following code is executed?
int [] nums = { 1, 2, 3, 4, 5 };
int temp = nums[2];
nums[2] = nums[4];
nums[4] = temp;
15-5-8: Which option will evaluate to true, if and only if both a and b are false?
15-5-9: What is the number 1011011011010100 (Binary) in hexadecimal?
15-5-10: What does the function ‘’mystery’’ do?
public void mystery (String tester)
{
for (int i = tester.length(); i >= 0; i--)
{
System.out.print(tester.charAt(i));
}
System.out.println("");
}
15-5-11: After the following code is executed, what does the variable mystery hold?
public class mysterious
{
public static void main (String[] args)
{
String mystery;
String starter = "Hello World!";
for(int i = 0; i < starter.length(); i++)
{
if(i % 2 == 0)
{
mystery += starter.charAt(i);
}
}
}
}
15-5-12: Which will cause the longest execution of a sequential search looking for a value in an array of 10 integers?
15-5-13: Which of the following reasons for using an inheritance hierarchy are valid?
I. Methods from a superclass can be used in a subclass without rewriting
or copying code.
II. An Object from a subclass can be passed as an argument to a method that takes an object of the superclass
III. Objects from subclasses can be stored in the same array
IV. All of the above
V. None of the above
15-5-14: Consider the following method and if int[] a = {8, 3, 1}
, what is the value in a[1]
after m1(a);
is run?
public int m1(int[] a)
{
a[1]--;
return (a[1] * 2);
}
15-5-15: What are the values of a
and b
after the for
loop finishes?
int a = 10, b = 3, t;
for (int i = 1; i <= 6; i++)
{
t = a;
a = i + b;
b = t - i;
}
15-5-16: Consider the following code. What string is referenced by s1
after the code executes?
String s1 = "Hi There";
String s2 = s1;
String s3 = s2;
String s4 = s1;
s2 = s2.toLowerCase();
s3 = s3.toUpperCase();
s4 = null;
15-5-17: Consider the following code segment. What value is in sum after this code executes?
int[][] matrix = { {1,1,2,2},{1,2,2,4},{1,2,3,4},{1,4,1,2} };
int sum = 0;
int col = matrix[0].length - 2;
for (int row = 0; row < 4; row++)
{
sum = sum + matrix[row][col];
}
15-5-18: Consider the following code segment, what are the contents of mat after the code segment has executed?
int [][] mat = new int [3][4];
for (int row = 0; row < mat.length; row++)
{
for (int col = 0; col < mat[0].length; col++)
{
if (row < col)
mat[row][col] = 3;
else if (row == col)
mat[row][col] = 2;
else
mat[row][col] = 1;
}
}
15-5-19: Assume that temp
is an int
variable initialized to be greater than zero and that a
is an array of type int
. Also, consider the following code segment. Which of the following will cause an infinite loop?
for ( int k = 0; k < a.length; k++ )
{
while ( a[k] < temp )
{
a[k] *= 2;
}
}
15-5-20: 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");
}
}