23-24 APCS Unit Six Multiple Choice B
Name: _____________________________
1. Consider the following code segment from an insertion sort program.
for(int j = 1; j < arr.length; j++)
{
int insertItem = arr[j];
int k = j – 1;
while(k >= 0 && insertItem < arr[k])
{
arr[k + 1] = arr[k];
k--;
}
arr[k + 1] = insertItem;
/* end of loop */
}
Assume that array arr has been defined and initialized with the values {5, 4, 3, 2, 1}.
What are the values in array arr after two passes of the for loop (i.e., when j = 2 at the
point indicated by / * end of for loop * / ) ?
(A) {2, 3, 4, 5, 1}
(B) {3, 2, 1, 4, 5}
(C) {3, 4, 5, 2, 1} run in Java Visualizer if asked
(D) {3, 5, 2, 3, 1}
(E) {5, 3, 4, 2, 1}
23-24 APCS Unit Six Multiple Choice B
2. Assume that an array of integer values has been declared as follows and has
been initialized.
int[] arr = new int[10];
Which of the following code segments correctly interchanges the value of arr[0] and
arr[5]?
from {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} to {5, 1, 2, 3, 4, 0, 6, 7, 8, 9}
(A)
arr[0] = 5;
arr[5] = 0;
assigns arr[0] to five and assigns arr[5] to zero / not switched
(B)
arr[0] = arr[5];
arr[5] = arr[0];
arr[0] assigned arr[5] 5 / arr[5] assigned arr[0] 5 / both are now the same, not
switched
(C)
int k = arr[5];
arr[0] = arr[5];
arr[5] = k;
k assigned 5 arr[0] assigned 5 arr[5] assigned 5 / both are the same, not switched
(D)
int k = arr[0];
arr[0] = arr[5];
arr[5] = k;
k assigned 0 arr[0] assigned 5 arr[5] assigned 0 / switched
(E)
int k = arr[5];
arr[5] = arr[0];
arr[0] = arr[5];
23-24 APCS Unit Six Multiple Choice B
k assigned 5 arr[5] assigned 0 arr[0] assigned 0 / not switched
23-24 APCS Unit Six Multiple Choice B
3. Consider the following method that is intended to return true if an array of
integers is arranged in decreasing order and return false otherwise.
/** @param nums an array of integers
* @return true if the values in the array appear in decreasing order
* false otherwise
*/
public static boolean isDecreasing(int[] nums)
{
/*missing code */
}
int[] numbers = new int[]{5, 4, 3, 2, 1};
Which of the following can be used to replace /* missing code */ so that isDecreasing will
work as intended?
I.
for (int k = 0; k < nums.length; k++) k = 0 / less than / length
{
if (nums[k] <= nums[k + 1]) [k] / [k + 1] / is 5 <= 4? (false)
return false; this statement never runs
}
return true;
ArrayIndexOutOfBoundsException – [k + 1] eventually is five
II.
for (int k = 1; k < nums.length; k++) k = 1 / less than / length
{
if (nums[k - 1] <= nums[k]) [k] / [k]/ is 5 < = 4? (false)
return false; this statement never runs
}
return true;
true
III.
for (int k = 0; k < nums.length - 1; k++) k = 0 / less than / length - 1
{
if (nums[k] <= nums[k + 1]) [k] / [k + 1] / is 5 <= 4? (false)
return false;
else
return true; this statement runs during the first iteration
}
return true; this statement never runs
true when first and second are in order, then returns
23-24 APCS Unit Six Multiple Choice B
(A) I only
(B) II only
(C) III only
(D) I and III
(E) II and III
23-24 APCS Unit Six Multiple Choice B
4. Consider the following class definition.
public class Book
{
private int pages;
public int getPages()
{
return pages;
}
// There may be instance variables, constructors, and methods
not shown.
}
The following code segment is intended to store in maxPages the greatest number of
pages found in any Book object in the array bookArr.
Book[] bookArr = { /* initial values not shown */ };
int maxPages = bookArr[0].getPages();
for (Book b : bookArr)
{
/* missing code */
}
23-24 APCS Unit Six Multiple Choice B
Which of the following can replace /* missing code */ so the code segment works as
intended?
(A)
if (b.pages > maxPages) instance variable pages is private
{ must use method
maxPages = b.pages;
}
(B)
if (b.getPages() > maxPages)
{
maxPages = b.getPages();
}
(C)
if (Book[b].pages > maxPages) cannot be a Book object / pages is private
{
maxPages = Book[b].pages;
}
(D)
if (bookArr[b].pages > maxPages) cannot be a Book object / pages is
private
{
maxPages = bookArr[b].pages;
}
(E)
if (bookArr[b].getPages() > maxPages) cannot use Book object
{
maxPages = bookArr[b].getPages();
}
23-24 APCS Unit Six Multiple Choice B
5. On Sunday night, a meteorologist records predicted daily high
temperatures, in degrees Fahrenheit, for the next seven days. At the end of
each day, the meteorologist records the actual daily high temperature, in
degrees Fahrenheit. At the end of the seven-day period, the meteorologist
would like to find the greatest absolute difference between a predicted
temperature and a corresponding actual temperature.
Consider the following method, which is intended to return the greatest absolute
difference between any pair of corresponding elements in the int arrays pred and act.
/** Precondition: pred and act have the same non-zero length. */
public static int diff(int[] pred, int[] act)
{
int num = Integer.MIN_VALUE;
for (int i = 0; i < pred.length; i++)
{
/* missing code */
}
return num;
}
23-24 APCS Unit Six Multiple Choice B
Which of the following code segments can be used to replace /* missing code */ so that
diff will work as intended?
(A)
if (pred[i] < act[i]) not absolute difference
{
num = act[i] - pred[i];
}
(B)
if (pred[i] > act[i]) not absolute difference
{
num = pred[i] - act[i];
}
(C)
if (pred[i] - act[i] > num) not absolute difference
{
num = pred[i] - act[i];
}
(D)
if (Math.abs(pred[i] - act[i]) < num) num is MIN_VALUE not MAX_VALUE
{
num = Math.abs(pred[i] - act[i]);
}
(E)
if (Math.abs(pred[i] - act[i]) > num)
{
num = Math.abs(pred[i] - act[i]);
}
23-24 APCS Unit Six Multiple Choice B
6. Consider the following incomplete method, which is intended to return the
longest string in the string array words. Assume that the array contains at
least one element.
public static String longestWord(String[] words)
{
/* missing declaration and initialization */
for (int k = 1; k < words.length; k++)
{
if (words[k].length() > longest.length())
{
longest = words[k];
}
}
return longest;
}
Which of the following can replace /* missing declaration and initialization */ so that the
method will work as intended?
(A) int longest = 0; should be a string not an integer (method return type)
(B) int longest = words[0].length(); should be a string not an integer (method return
type)
(C) String longest = ""; empty string / length = 0
(D) String longest = words[0];
(E) String longest = words[1]; would skip string at index 0
23-24 APCS Unit Six Multiple Choice B
7. Consider the following code segment.
int[] arr = {1, 2, 3, 4, 5, 6, 7};
for (int i = 1; i < arr.length; i += 2) i would be 1, 3, 5, 7
{
arr[i] = arr[i - 1];
}
Which of the following represents the contents of the array arr after the code segment is
executed?
Only elements 1, 3, 5 would change / 2, 4, 6 would change
(A) {0, 1, 2, 3, 4, 5, 6}
(B) {1, 1, 1, 1, 1, 1, 1}
(C) {1, 1, 3, 3, 5, 5, 7}
(D) {1, 2, 3, 4, 5, 6, 7}
(E) {2, 2, 4, 4, 6, 6, 7}
23-24 APCS Unit Six Multiple Choice B
8. Consider the following instance variable and method.
private int[] numbers;
public void mystery(int x)
{
for(int k = 1; k < numbers.length; k = k + x) k will be 1, 4, 7, 10
{
numbers[k] = numbers[k – 1] + x; k = 1 [1]34 = [0]17 + 3 =
[1]20
}
}
Assume that numbers has been initialized with the following values.
{17, 34, 21, 42, 15, 69, 48, 25, 39}
Which of the following represents the order of the values in numbers as a result of the
call mystery(3)?
(A) {17, 20, 21, 42, 45, 69, 48, 51, 39}
(B) {17, 20, 23, 26, 29, 32, 35, 38, 41}
(C) {17, 37, 21, 42, 18, 69, 48, 28, 39}
(D) {20, 23, 21, 42, 45, 69, 51, 54, 39}
(E) {20, 34, 21, 45, 15, 69, 51, 25, 39}
23-24 APCS Unit Six Multiple Choice B
9. The question refers to the following data field and method.
private int[] arr;
// precondition: arr.length > 0
public void mystery()
{
int s1 = 0;
int s2 = 0;
for (int k = 0; k < arr.length; k++)
{
int num = arr[k];
if ((num > 0) && (num % 2 == 0)) positive and even
s1 += num;
else if (num < 0) negative
s2 += num;
}
System.out.println(s1);
System.out.println(s2);
}
Which of the following best describes the value of s1 output by the method mystery?
(A) The sum of all positive values in arr
(B) The sum of all positive even values in arr
(C) The sum of all positive odd values in arr
(D) The sum of all values greater than 2 in arr
(E) The sum of all values less than 2 in arr
23-24 APCS Unit Six Multiple Choice B
10. Consider the following method.
public static int mystery(int[] arr)
{
int x = 0;
for(int k = 0; k < arr.length; k = k + 2) k will be 0, 2, 4, 6, 8
x = x + arr[k];
return x;
}
Assume that the array nums has been declared and initialized as follows.
int[] nums = {3, 6, 1, 0, 1, 4, 2};
if k is 0, 2, 4, 6 add 3 + 1 + 1 + 2 = 7
What value will be returned as a result of the call mystery(nums)?
(A) 5
(B) 6
(C) 7
(D) 10
(E) 17
23-24 APCS Unit Six Multiple Choice B
11. Consider the following method, which is intended to return the average
(arithmetic mean) of the values in an integer array. Assume the array contains
at least one element.
public static double findAvg(double[] values)
{
double sum = 0.0;
for (double val : values)
{
sum += val;
}
return sum / values.length;
}
Which of the following preconditions must be true about the array values so that the
method works as intended?
(A) The array values must be sorted in ascending order.
(B) The array values must be sorted in descending order.
(C) The array values must have only one mode.
(D) The array values must not contain values whose sum is not 0.
(E) No precondition is necessary; the method will always work as intended.
23-24 APCS Unit Six Multiple Choice B
12. Consider the following method.
public static int getValue(int[] data, int j, int k)
{
return data[j] + data[k];
}
Which of the following code segments, when appearing in another method in the same
class as getValue, will print the value 70?
(A)
int arr = {40, 30, 20, 10, 0}; not an array; missing []
System.out.println(getValue(arr, 1, 2));
(B)
int[] arr = {40, 30, 20, 10, 0};
System.out.println(getValue(arr, 1, 2)); adds 30 + 20 = 50
(C)
int[] arr = {50, 40, 30, 20, 10};
System.out.println(getValue(arr, 1, 2)); adds 40 + 30 = 70
(D)
int arr = {40, 30, 20, 10, 0}; not an array; missing []
System.out.println(getValue(arr, 2, 1));
(E)
int arr = {50, 40, 30, 20, 10}; not an array; missing []
System.out.println(getValue(arr, 2, 1));
23-24 APCS Unit Six Multiple Choice B
13. Consider the following method, which is intended to return an array of
integers that contains the elements of the parameter arr arranged in reverse
order. For example, if arr contains {7, 2, 3, -5}, then a new array containing {-
5, 3, 2, 7} should be returned and the parameter arr should be left unchanged.
public static int[] reverse(int[] arr)
{
int[] newArr = new int[arr.length];
for (int k = 0; k < arr.length; k++)
{
/* missing statement */
}
return newArr;
}
Which of the following statements can be used to replace /* missing statement */ so that
the method works as intended?
(A) newArray[k] = arr[-k]; negative array index
(B) newArray[k] = arr[k - arr.length]; negative array index when k = 0
(C) newArray[k] = arr[k - arr.length - 1]; negative array index when k = 0
(D) newArray[k] = arr[arr.length - k]; negative array index when k = 0
(E) newArray[k] = arr[arr.length - k - 1]; array length of 1 / 1 – 0 – 1 = 0
23-24 APCS Unit Six Multiple Choice B
14. Consider the following method.
public static String[] strArrMethod(String[] arr)
{
String[] result = new String[arr.length];
for (int j = 0; j < arr.length; j++) j is 0, 1, 2, 3
{
String sm = arr[j];
for (int k = j + 1; k < arr.length; k++) k is 2, 3, 4
{
if (arr[k].length() < sm.length()) compare lengths
{
sm = arr[k]; // Line 12
}
}
result[j] = sm;
}
return result;
}
Consider the following code segment.
String[] testOne = {"first", "day", "of", "spring"};
String[] resultOne = strArrMethod(testOne);
What are the contents of resultOne when the code segment has been executed?
(A) {"day", "first", "of", "spring"} alphabetized
(B) {"of", "day", "first", "spring"} increasing length of string
(C) {"of", "day", "of", "spring"} if k = j + 2
(D) {"of", "of", "of", "spring"} compare [0] to all others, find shortest, replace / compare
[1]
to all others, find shortest, replace / compare [2] to all
others, find
shortest, replace
23-24 APCS Unit Six Multiple Choice B
(E) {"spring", "first", "day", "of\"} decreasing string length
15. Consider the following method.
public static String[] strArrMethod(String[] arr)
{
String[] result = new String[arr.length];
for (int j = 0; j < arr.length; j++)
{
String sm = arr[j];
for (int k = j + 1; k < arr.length; k++)
{
if (arr[k].length() < sm.length())
{
sm = arr[k]; // Line 12
}
}
result[j] = sm;
}
return result;
}
Consider the following code segment.
String[] testTwo = {"last", "day", "of", "the", "school", "year"};
String[] resultTwo = strArrMethod(testTwo);
How many times is the line labeled // Line 12 in the strArrMethod executed as a result of
executing the code segment?
(A) 4 times only executed when a smaller string is found
(B) 5 times
(C) 6 times
(D) 15 times
(E) 30 times
23-24 APCS Unit Six Multiple Choice B