# 7.11.4. Easier Search/Sort Multiple Choice QuestionsΒΆ

These problems are easier than most of those that you will usually see on the AP CS A exam.

- -1
- This value is returned if the target is not in the list since this is a sequential search.
- 0
- This would be true if the target was 90 since this is a sequential search.
- 1
- This would be true if the target was -30 since this is a sequential search.
- 2
- This is a sequential search that returns the index where the target appears in the elements list
- 50
- A sequential search returns the index, not the value. What is the index of the 50?

7-11-4-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;
}
```

You can step through the code above by clicking on the following Ex-12-7-1.

- -1
- A sequential search returns -1 if the target value is not found in the list.
- 0
- This would be true if the target was 90 since this is a sequential search.
- 1
- This would be true if the target was -30 since this is a sequential search.
- 2
- This would be true if the target was
- -20
- A sequential search returns negative one when the value isn't found in the list.

7-11-4-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;
}
```

You can step through the code above by clicking on the following Ex-12-7-2.

- 1
- This would be true if we were looking for 23.
- 2
- 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.
- 3
- This would be true if we were looking for 10.

7-11-4-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;
}
```

You can step through the code above by clicking on the following Ex-12-7-3.

- selection sort
- A selection sort has nested for loops.
- insertion sort
- An insertion sort has a while loop inside a for loop.
- merge sort
- A merge sort has a recursive call to mergeSortHelper in mergeSortHelper.

7-11-4-4: Which sort contains a recursive call?

- 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.

7-11-4-5: Under what condition will an ascending insertion sort execute the slowest?