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 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.
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++;
}
}
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].
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;
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.
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];
}
}
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.