The following problems are harder than what you will probably see on the AP CSA exam. They come from research in computer science education and test your ability to trace and understand complex code with loops, arrays, and conditionals. They are used with permission from Raymond Lister of the University of Technology, Sydney, Australia.
Click the โStartโ button when you are ready to begin the exam, but only then as you can only take the exam once. Click the โPauseโ button to pause the exam (you will not be able to see the questions when the exam is paused). Click on the โFinish Examโ button at the end only when you are done. It will display the number correct, number wrong, and number skipped after the โFinish Examโ button.
This would be true if the while loop continued as long as i1 and i2 where greater than or equal to 0.
2
This loops and only increments count when the same value is in x1 and x2, but it doesnโt compare the values at index 0 since it stops when either index is 0.
1
It may appear that the indices get out of synchronization, but check out the else if and else code.
0
The very first time through the loop the values at the two indices are equal and count is incremented.
int [] x = {1, 2, 3, 3, 3};
boolean b[] = new boolean[x.length];
for (int i = 0; i < b.length; i++)
b[i] = false;
for (int i = 0; i < x.length; i++)
b[ x[i] ] = true;
int count = 0;
for (int i = 0; i < b.length; i++)
{
if (b[i] == true)
{
count++;
}
}
1
This would be true if the second loop only executed one time, but it executes for all values in x.
2
This would be true if there were only 2 distinct values in x.
3
This changes b[x[i]] to true and then counts the number of true in b. Since x only has 3 distinct values in it the answer will be 3.
4
This would be true if there were 4 distinct values in x.
5
This would be true if it was ``b[i] = true`` instead of ``b[x[i]] = true``.
int[ ] x = {0, 1, 2, 3};
int temp;
int i = 0;
int j = x.length - 1;
while (i < j)
{
temp = x[i];
x[i] = x[j];
x[j] = 2 * temp;
i++;
j--;
}
{3,2,2,0}
This loops 2 times. During the first loop it copies x[3] to x[0] and sets x[3] to 2 times the original value of x[0]. In the second loop it copies x[2] to x[1] and sets x[2] to 2 times the original value in x[1].
{0,1,2,3}
These are the initial values for x, but they are changed since i starts out at 0 and j starts out as 3 and i is less than j.
{3,2,1,0}
This would be true if the code simply reversed the values in the array, but what happens when x[j] = 2 * temp?
{0,2,4,6}
This would be true if the code just multiplied all the original values by 2. Is that what it does? Why would you need to use temp for that?
{6,4,2,0}
This would be true if the code multiplied the original values by 2 and reversed the values. Is that what it does? The loop only continues while i is less than j, so it doesnโt loop through all the values in x.
public static boolean isSorted(int[] x)
{
//missing code goes here
}
A.
boolean b = true;
for (int i=0 ; i < x.length - 1; i++)
{
if ( x[i] > x[i+1] )
b = false;
else
b = true;
}
return b;
B.
for (int i=0; i < x.length - 1; i++)
{
if (x[i] > x[i+1] )
return false;
}
return true;
C.
boolean b = false;
for (int i=0; i<x.length - 1; i++)
{
if (x[i] > x[i+1] )
b = false;
}
return b;
D.
boolean b = false;
for (int i=0;i<x.length - 1;i++)
{
if (x[i] > x[i+1] )
b = true;
}
return b;
E.
for (int i=0;i<x.length - 1;i++)
{
if (x[i] > x[i+1] )
return true;
}
return false;
A
This wonโt work since each time through the loop b is reset based on the last comparison.
B
This will return false if it finds a value at i that is larger than the value at i+1 and otherwise it returns true.
C
There is no way for b to become true.
D
This sets b to true when it finds elements that are out of order.
E
This returns as soon as it finds a value that is out of order, but returns true instead of false.
int[] x = {2, 1, 4, 5, 7};
int limit = 7;
int i = 0;
int sum = 0;
while ((sum<limit) && (i<x.length))
{
sum += x[i];
i++;
}
0
Each time the loop executes i is incremented and it executes at least once.
1
Does this code only execute 1 time?
2
This code will loop till sum is not less than limit. It adds the value at i of x each time to sum so sum isnโt 7 until the 3rd time through the loop.
3
This will loop three times till sum is 7 and so i will be 3.
If any two numbers in an array of integers, not necessarily consecutive numbers in the array, are out of order (i.e. the number that occurs first in the array is larger than the number that occurs second), then that is called an inversion. For example, consider an array x that has the values {1, 4, 3, 2}. Then there are three inversions since 4 is greater than both 3 and 2 and 3 is greater than 2. Which of the following can be used to replace the missing code so that the code correctly counts the number of inversions?
Which of the following correctly copies all the even numbers from array1 to array2 in the same order as they are in array1 without any errors? Assume that array2 is large enough for all the copied values.
A.
int a2 = 0;
for (int a1=0 ; a1 < array1.length ; a1++)
{
// if array1[a1] is even
if (array1[a1] % 2 == 0)
{
// array1[a1] is even,
// so copy it
a2++;
array2[a2] = array1[a1];
}
}
B.
int a2 = 0;
for (int a1=0 ; a1 < array1.length ; a1++)
{
// if array1[a1] is even
if (array1[a1] % 2 == 0)
{
// array1[a1] is even,
// so copy it
array2[a2] = array1[a1];
a2++;
}
}
C.
int a2 = 0;
for ( int a1=0 ; a1 <= array1.length ; a1++)
{
// if array1[a1] is even
if (array1[a1] % 2 == 0)
{
// array1[a1] is even,
// so copy it
array2[a2] = array1[a1];
a2++;
}
}
D.
int a2 = 0;
for (int a1=0 ; a1 <= array1.length ; a1++)
{
// if array1[a1] is even
if (array1[a1] % 2 == 0)
{
// array1[a1] is even,
// so copy it
a2++;
array2[a2] = array1[a1];
}
}
A
This increments a2 before copying the value into array2 and so puts it in the wrong place.
B
This will copy all the even values in array1 to array2 and put them in the same position as they were in array1.
C
This will cause an out of bounds error.
D
This increments a2 before copying the value into array2 and so puts it in the wrong place.
This copies the value from array1[a1] to array2[a2] but only if the value at array1[a1] is greater than or equal to 2. So it copies the 4 and 3. Notice that a2 starts at 0 and a1 starts at 1.
{4, 1, 3, 0}
This would be true except that a2 is only incremented if the copy occurs.
{2, 4, 3, 0}
Walk through the very first iteration of the loop and notice that after the first iteration the first value in array2 is 4.
{2, 4, 1, 3}
This would be true if we were asking for the values in array1.