Skip to main content
Contents Index
Dark Mode Prev Up Next Scratch ActiveCode Profile
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 4.45 Search/Sort Multiple-Choice Exercises
Section 4.45.1 Easier Search/Sort Multiple Choice Questions
These problems are easier than most of those that you will usually see on the AP CSA exam.
Activity 4.45.1 .
What would the following code return from mystery([90, -30, 50], 50)?
public static int mystery(int[] elements, int target)
{
for (int j = 0; j < elements.length; j++)
{
if (elements[j] == target)
{
return j;
}
}
return -1;
}
This value is returned if the target is not in the list since this is a sequential search.
This would be true if the target was 90 since this is a sequential search.
This would be true if the target was -30 since this is a sequential search.
This is a sequential search that returns the index where the target appears in the elements list
A sequential search returns the index, not the value. What is the index of the 50?
You can step through the code above by clicking on the following
Ex-12-7-1 .
Activity 4.45.2 .
What would the following code return from mystery([90, -30, 50], -20)?
public static int mystery(int[] elements, int target)
{
for (int j = 0; j < elements.length; j++)
{
if (elements[j] == target)
{
return j;
}
}
return -1;
}
A sequential search returns -1 if the target value is not found in the list.
This would be true if the target was 90 since this is a sequential search.
This would be true if the target was -30 since this is a sequential search.
This would be true if the target was
A sequential search returns negative one when the value isnβt found in the list.
You can step through the code above by clicking on the following
Ex-12-7-2 .
Activity 4.45.3 .
Consider the
binarySearch
method below. How many times would the while loop execute if you first do int[] arr = {2, 10, 23, 31, 55, 86} and then call binarySearch(arr,2)?
public static int binarySearch(int[] elements, int target) {
int left = 0;
int right = elements.length - 1;
while (left <= right)
{
int middle = (left + right) / 2;
if (target < elements[middle])
{
right = middle - 1;
}
else if (target > elements[middle])
{
left = middle + 1;
}
else {
return middle;
}
}
return -1;
}
This would be true if we were looking for 23.
It first compares 23 at index 2 (5 / 2 is 2) to 2. The second time it compares the 2 at index 0 (1 / 2 = 0) to 2 and returns 0.
This would be true if we were looking for 10.
You can step through the code above by clicking on the following
Ex-12-7-3 .
Activity 4.45.4 .
Which sort contains a recursive call?
A selection sort has nested for loops.
An insertion sort has a while loop inside a for loop.
A merge sort has a recursive call to mergeSortHelper in mergeSortHelper.
Activity 4.45.5 .
Under what condition will an ascending insertion sort execute the slowest?
If the data is already sorted in ascending order
If the data is already sorted in the correct order you donβt need to move any values.
If the data is already sorted in descending order
All values will have to be moved multiple times since the data was sorted into descending order.
It will always take the same amount of time to execute
This would be true if it was a selection sort.
Section 4.45.2 Medium Search/Sort Multiple Choice Questions
These problems are similar to those you will see on the AP CSA exam.
Activity 4.45.1 .
Which of the following correctly shows the iterations of an ascending (from left to right) insertion sort on an array with the following elements: {7,3,8,5,2}?
{3,7,8,5,2}, {3,7,8,5,2}, {3,5,7,8,2}, {2,3,5,7,8}
The insertion sort starts at index 1 and inserts each value into the sorted list to the left by moving any larger values right.
{2,3,8,5,7}, {2,3,8,5,7}, {2,3,5,8,7}, {2,3,5,7,8}
This would be true if it was a selection sort.
{3,7,8,5,2}, {3,5,7,8,2}, {2,3,5,7,8}
This looks like an insertion sort, but it is missing one step.
{2,3,8,5,7}, {2,3,5,8,7}, {2,3,5,7,8}
This looks like a selection sort, but it is missing one step.
{2,7,3,8,5}, {2,3,7,8,5}, {2,3,5,7,8}
This is more like a selection sort, but not a correct one.
You can step through an insertion sort with this data by clicking on the following
Ex-12-8-1 .
Activity 4.45.2 .
What is printed when the following main method is executed?
public class Searcher
{
private int[] arr = {1, 3, 5, 8, 9};
public int mystery(int low, int high, int num)
{
int mid = (low + high) / 2;
if (low > high)
{
return -1;
}
else if (arr[mid] < num)
{
return mystery(mid + 1, high, num);
}
else if (arr[mid] > num)
{
return mystery(low, mid - 1, num);
} else return mid;
}
public static void main(String[] args)
{
Searcher s = new Searcher();
System.out.println(s.mystery(0, 4, 3));
}
}
This would be true if the third value was something that wasnβt in the array.
This would be true if the third value was 1
This is a binary search and it returns the index of the value 3, which is 1.
This would be true if the third value was 5.
This would be true if the third value was 8.
You can step through the code above by clicking on the following
Ex-12-8-2 .
Activity 4.45.3 .
Which of the following correctly shows the iterations of an ascending (from left to right) selection sort on an array with the following elements: {10, 6, 3, 2, 8}?
{6,10,3,2,8}, {3,6,10,2,8}, {2,3,6,10,8}, {2,3,6,8,10}
This would be true if it was an insertion sort.
{6,10,3,2,8}, {3,6,10,2,8}, {2,3,6,8,10}
This would be true if it was an insertion sort, but you are also missing a step.
{2,6,3,10,8}, {2,3,6,10,8}, {2,3,6,8,10}
This is almost right, but is missing one step.
{2,6,3,10,8}, {2,3,6,10,8}, {2,3,6,10,8}, {2,3,6,8,10}
This is the result from a selection sort.
You can step through the code above by clicking on the folloiwng
Ex-12-8-3 .
Activity 4.45.4 .
Which of the following could be used to replace // missing code // in the code so that the method always sorts the array
elem
in ascending order?
public class Searcher
{
public static void sort(int[] elem)
{
for (int j = 0; j < elem.length - 1; j++)
{
int minIndex = j;
for (// missing code //)
{
if (elem [k] < elem [minIndex])
{
minIndex = k;
}
}
int temp = elem[j];
elem[j] = elem[minIndex];
elem[minIndex] = temp;
}
}
public static void main(String[] args)
{
int[] nums = {28, -3, 2, 14, 30};
Searcher.sort(nums);
}
}
int k = j - 1; k >= 0; k--
The inner loop starts at the outer loop value plus one, not minus one.
int k = j + 1; k < elem.length; k++
The inner loop starts at the outer loop value plus one and ends at the last element.
int k = j; k < elem.length; k++
The inner loop should start at the outer loop value plus one.
The inner loop should start at the outer loop value plus one and increment.
int k = j - 1; k > 0; k--
The inner loop should start at the outer loop value plus one and increment.
You can step through the code above (with answer a in place of the missing code) by clicking on the following
Ex-12-8-4 .
Activity 4.45.5 .
What would test return if a = {1,2,3,4} and v = 3?
public static int test(int[] a, int v)
{
for (int i = 0; i < a.length; i++)
{
if (a[i] == v)
return i;
else return -1;
}
}
This would be true if the sequential search code was okay and v was a value that wasnβt in the array, but the code is incorrect. The return -1 should be outside of the for loop.
This would be true if v was 1 and the code was correct for a sequential search.
This would be true if v was 2 and the code was correct for a sequential search.
This would be true if the code was correct for a sequential search, but it returns -1 inside the for loop instead of outside of it.
The code will not compile
This method wonβt compile because it is supposed to return an integer and if the for loop doesnβt execute it will not return anything. The return -1 should be outside the for loop to make this sequential search work as intended.
You can see this code at the following
Ex-12-8-5 .
Section 4.45.3 Hard Search/Sort Multiple Choice Questions
These problems are harder than those you will see on the AP CSA exam.
Activity 4.45.1 .
What is printed when the following main method is executed? The break; statement used in this code breaks out of or terminates the loop at that point. It is not used on the AP CSA exam.
public class AlphaSort
{
public static void main(String[] args)
{
int i, j;
String key;
String[] letters = {"E", "D", "C", "B", "A", "B"};
for (j = 1; j < letters.length; j++)
{
key = letters[j];
i = j - 1;
while (i >= 0)
{
if (key.compareTo(letters[i]) > 0)
{
break;
}
letters[i + 1] = letters[i];
i--;
}
letters[i + 1] = key;
}
for (int t = 0; t < letters.length; t++)
{
System.out.print((letters[t]) + "");
}
}
}
This would be true if the for loop inside the main method did not interate through every value in the array.
This would be true if the conditional statement inside the for loop stated "if (key.compareTo(letters[i]) < 0)", because that would put the array in a reverse alphabetical order.
This is an insertion sort which sorts the array in alphabetical order using the compareTo() method.
This would be true if array was not modified at all in the main method.
This would be true if the conditional statement inside the for loop stated "if (key.compareTo(letters[i]) < 0)" and if the loop did not iterate through every item of the letters array, because that would put the array in a reverse alphabetical order.
You can step through the code above by clicking on the following link
Ex-12-8-1 .
Activity 4.45.2 .
What is printed when the following main method is executed?
public class NumberCount
{
public static void main(String[] args)
{
int count = 0;
int[] numbers = {-5, 4, -5, 3, -2, -4};
for (int j = 0; j < numbers.length; j++)
{
if (numbers[j] < 0 && numbers[j] % 2 != 0)
{
count++;
}
}
System.out.println(count);
}
}
This would be true if the if statement was not trying to check if the numbers in the array were negative and odd.
This answer is correct because the for loop iterates through every element and increments the count if the current number is negative and odd.
This may be a result of misunderstanding the question, as 12 cannot be an answer because the array length itself is only 6.
This would be true if the code was looking for the numbers in the array that were positive and odd.
You can step through the code above by clicking on the following link
Ex-12-8-2 .
Activity 4.45.3 .
What is printed when the following main method is executed?
public class GuestList
{
public static void main(String[] args)
{
int count = 0;
String[] guestList = {"Anna", "Briana", "Alex", "John"};
String subj1 = null;
String subj2 = null;
for (int j = 0; j < guestList.length; j++)
{
subj1 = guestList[j].substring(0, 1);
subj2 = guestList[j].substring(guestList[j].length() - 1);
if (subj1.equalsIgnoreCase(subj2))
{
count--;
}
else if (subj1.equalsIgnoreCase("a"))
{
count++;
}
}
System.out.println(count);
}
}
This would be true if there were three strings in the array that had the same first letter as the last letter.
This would be true if there were four strings in the array that had the same first letter as the last letter.
This would be true if there had been four strings in the array that had the first letter as an A and those stringsβ last letter was not an A.
This is the correct answer. The for loop is iterating through every element in the guestList array and the first if statement is checking to see if the current element in the array starts with the same letter and ends with the same letter. The variable, count decreases by one if that is true. However if that is false, the program goes to the else if statment and checks to see if the first letter is an A. If that is true count increases by one.
You can step through the code above by clicking on the following link
Ex-12-8-3 .
Activity 4.45.4 .
What is printed when the following main method is executed?
public class OddEvenMod
{
public static void main(String[] args)
{
int[] arr = {8, 7, 7, 3, 4, 1};
for (int i = 0; i < arr.length; i++)
{
if (arr[i] % 2 == 0)
{
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
}
}
for (int t = 0; t < arr.length; t++)
{
System.out.print((arr[t]) + ",");
}
}
}
This would be true if the array was not modified at all.
This is the correct answer. The for loop is iterating through every element in the array. The if statement is checking to see if the current element is even or odd. If it is even, then the first element of the array and the current element will swap places in the array.
This would be true if the loop had brought all the even numbers to the beginning of the array.
This would be true if the if statement had said: if(arr[i] % 2 == 1).
You can step through the code above by clicking on the following link
Ex-12-8-4 .
Activity 4.45.5 .
What is printed when the following main method is executed?
public class PrimeOrNot
{
private static boolean check(int n)
{
for (int i = 2; i < n; i++)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
public static void main(String[] args)
{
int[] arr = {5, 3, 2, 9, 3, 4};
for (int i = 0; i < arr.length; i++)
{
if (check(arr[i]))
{
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
}
}
for (int t = 0; t < arr.length; t++)
{
System.out.print((arr[t]) + ",");
}
}
}
This is the correct answer. The check method is using a for loop and an if statement to return true if the parameter is prime and false if it is not prime. In the main method, the for loop iterates through every element in the array and checks to see if it is prime. If it is prime, then the program will swap that element with the first element in the array.
This would be true if the if statement had said: if(!check(arr[i])).
This would be true if the array had not been modified at all.
This would be true if the final for loop did not iterate through every element in the array.
You can step through the code above by clicking on the following link
Ex-12-8-5 .
Activity 4.45.6 .
What is printed when the following main method is executed?
public class GradeSort
{
public static void main(String[] args)
{
String[] names = {"Anna", "John", "Billy", "Bob", "Roger", "Dominic"};
int[] grades = {93, 100, 67, 84, 86, 93};
int i, j, first, temp;
String temp2;
for (i = grades.length - 1; i > 0; i--)
{
first = 0;
for (j = 1; j <= i; j++)
{
if (grades[j] < grades[first])
{
first = j;
}
}
temp = grades[first];
grades[first] = grades[i];
grades[i] = temp;
temp2 = names[first];
names[first] = names[i];
names[i] = temp2;
}
for (int t = 0; t < names.length; t++)
{
System.out.print((names[t]) + " ");
}
}
}
Anna John Billy Bob Roger Dominic
This would be true if the program did not modify the names array at all.
John Dominic Anna Roger Bob Billy
This is the correct answer. The program is ordering the grades array from greatest to least as well as keeping the names with the grades.
Billy Bob Roger Anna Dominic John
This would be true if the program sorted the grades array from the smallest value to the largest value.
Anna John Billy Bob Roger
This would be true if the program did not modify the names array and if the for loop at the end of the program did not output all the values of the array.
You can step through the code above by clicking on the following link
Ex-12-8-6 .
Activity 4.45.7 .
What is printed when the following main method is executed?
public class DivisibleBy2or3
{
private static boolean divCheck(int n)
{
if (n % 2 == 0 || n % 3 == 0)
{
return true;
}
return false;
}
public static void main(String[] args)
{
int[] arr = {6, 7, 17, 3, 2, 9, 1, 5};
for (int i = 0; i < arr.length; i++)
{
if (divCheck(arr[i]))
{
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
}
}
for (int t = 0; t < arr.length; t++)
{
System.out.print((arr[t]) + " ");
}
}
}
This would be true if the program had not modified the array at all.
This would be true if the loop was moving the position of odd numbers in the array to arr.length-1.
This would be true if the array was printed in the reversed order.
This is the correct answer, because the divCheck method is checking to see if the values in the array are divisible by 2 or 3. If they are, they are swapped with the value at the first position (index 0).
You can step through the code above by clicking on the following link
Ex-12-8-7 .
You have attempted
of
activities on this page.