0% found this document useful (0 votes)
5 views14 pages

Unit 6 Topic Questions - Array

The document contains a series of questions related to AP Computer Science A, focusing on Java programming concepts such as array manipulation, method implementation, and control structures. It includes code snippets and asks for modifications or implementations to achieve desired outcomes. The questions cover various topics including loops, conditionals, and method functionality within the context of Java programming.

Uploaded by

Tanisha Bentick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views14 pages

Unit 6 Topic Questions - Array

The document contains a series of questions related to AP Computer Science A, focusing on Java programming concepts such as array manipulation, method implementation, and control structures. It includes code snippets and asks for modifications or implementations to achieve desired outcomes. The questions cover various topics including loops, conditionals, and method functionality within the context of Java programming.

Uploaded by

Tanisha Bentick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

AP COMPUTER SCIENCE A Test Booklet

Unit 6 Topic Questions

1. Consider the following method.

public static void addOneToEverything(int[] numbers)


{
for (int j = 0; j < numbers.length; j++)
{
numbers[j]++;
}
}

Which of the following code segments, if any, can be used to replace the body of the method so that numbers
will contain the same values?

I.
for (int num : numbers)
{
num++;
}

II.
for (int num : numbers)
{
num[j]++;
}

III.
for (int num : numbers)
{
numbers[num]++;
}
(A) I only
(B) I and III only
(C) II and III only
(D) I, II, and III
(E) None of the code segments will return an equivalent result.

AP Computer Science A Page 1 of 14


Test Booklet

Unit 6 Topic Questions

2. SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.

Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are
called only when their preconditions are satisfied.
In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined
in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will
not receive full credit.

Some applications use a controlled vocabulary to describe, or tag, things. A controlled vocabulary is a limited set of
keywords from which appropriate tags can be chosen.

The Vocab class, shown below, contains methods used to analyze words in terms of their presence in a controlled
vocabulary. You will write two methods of the Vocab class.

public class Vocab


{
/** The controlled vocabulary for a Vocab object. */
private String[] theVocab = { /* contents not shown */ };

/** Searches for a string in theVocab. Returns true if its String


parameter str
* is an exact match to an element in theVocab and returns false
otherwise.
*/
public boolean findWord(String str)
{
/* implementation not shown */
}

/** Counts how many strings in wordArray are not found in theVocab,
as described in
* part (a).
*/
public int countNotInVocab(String[] wordArray)
{
/* to be implemented in part (a) */
}

/** Returns an array containing strings from wordArray not found in


theVocab,
* as described in part (b).
*/
public String[] notInVocab(String[] wordArray)
{
/* to be implemented in part (b) */
}

Page 2 of 14 AP Computer Science A


Test Booklet

Unit 6 Topic Questions

The countNotInVocab method returns an int that contains the number of words in its parameter wordArray that
are not found in the instance variable theVocab.

A helper method, findWord, has been provided. The findWord method searches for an individual string in
theVocab, returning true if an exact match between its String parameter and an element of theVocab is found,
and returning false otherwise.

(a) Write the countNotInVocab method. Assume that there are no duplicates in wordArray. You must use
findWord appropriately to receive full credit.

/** Counts how many strings in wordArray are not found in theVocab, as
described in
* part (a).
*/
public int countNotInVocab(String[] wordArray)

The notInVocab method returns an array of String objects that contains only elements of its parameter
wordArray that are not found in theVocab. The array that is returned by notInVocab should have exactly one
element for each word in wordArray that is not found in theVocab. Assume that there are no duplicates in
wordArray.

The following example illustrates the behavior of the notInVocab method.

theVocab:

"time" "food" "dogs" "cats" "health" "plants" "sports"

wordArray:

"dogs" "toys" "sun" "plants" "time"

Array returned by notInVocab:

"toys" "sun"

(b) Write the notInVocab method. Assume that there are no duplicates in wordArray. You must call findWord
and countNotInVocab appropriately in order to receive full credit.

/** Returns an array containing strings from wordArray not found in


theVocab,
* as described in part (b).

AP Computer Science A Page 3 of 14


Test Booklet

Unit 6 Topic Questions

*/
public String[] notInVocab(String[] wordArray)

3. Consider the following method.

public void changeIt(int[] arr, int index, int newValue)


{
arr[index] += newValue;
}

Which of the following code segments, if located in a method in the same class as changeIt, will cause the array
myArray to contain {0, 5, 0, 0} ?
int[] myArray = new int[4];
(A) changeIt(myArray, 1, 5);

int[] myArray = new int[4];


(B) changeIt(myArray, 2, 5);
int[] myArray = new int[4];
(C) changeIt(myArray, 5, 1);
int[] myArray = new int[5];
(D) changeIt(myArray, 1, 4);

int[] myArray = new int[5];


(E) changeIt(myArray, 1, 5);

Page 4 of 14 AP Computer Science A


Test Booklet

Unit 6 Topic Questions

4. SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.

Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are
called only when their preconditions are satisfied.
In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined
in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will
not receive full credit.

The divBySum method is intended to return the sum of all the elements in the int array parameter arr that are
divisible by the int parameter num. Consider the following examples, in which the array arr contains {4, 1,
3, 6, 2, 9}.

The call divBySum(arr, 3) will return 18, which is the sum of 3, 6, and 9, since those are the only integers
in arr that are divisible by 3.
The call divBySum(arr, 5) will return 0, since none of the integers in arr are divisible by 5.

Complete the divBySum method using an enhanced for loop. Assume that arr is properly declared and
initialized. The method must use an enhanced for loop to earn full credit.

/** Returns the sum of all integers in arr that are divisible by num
* Precondition: num > 0
*/
public static int divBySum(int[] arr, int num)

5. The method countTarget below is intended to return the number of times the value target appears in the
array arr. The method may not work as intended.

public int countTarget(int[] arr, int target)


{
int count = 0;
for (int j = 0; j <= arr.length; j++) // line 4
{
if (arr[j] == target)
{
count++;
}
}
return count;
}

Which of the following changes, if any, can be made to line 4 so that the method will work as intended?

AP Computer Science A Page 5 of 14


Test Booklet

Unit 6 Topic Questions

(A) Changing int j = 0; to int j = 1;


(B) Changing j <= arr.length; to j < arr.length;
(C) Changing j <= arr.length; to j < arr.length - 1;
(D) Changing j <= arr.length; to j < arr.length + 1;
(E) No change is necessary; the method works correctly as is.

6. Consider the following code segment, which is intended to print the sum of all elements of an array.

int[] arr = {10, 5, 1, 20, 6, 25};


int sum = 0;
for (int k = 0; k <= arr.length; k++)
{
sum += arr[k];
}
System.out.println("The sum is " + sum);

A runtime error occurs when the code segment is executed. Which of the following changes should be made so that
the code segment works as intended?
(A) The for loop header should be replaced with for (int k = 0; k < arr.length; k++).
(B) The for loop header should be replaced with for (int k = 0; k <= arr.length; k--).
The for loop header should be replaced with for (int k = 1; k <= arr.length -
(C)
1; k++).
(D) The statement in the body of the for loop should be replaced with sum += arr[0].
(E) The statement in the body of the for loop should be replaced with sum += arr[k - 1].

7. Consider the code segment below, where arr is a one-dimensional array of integers.

int sum = 0;
for (int n : arr)
{
sum = sum + 2 * n;
}
System.out.print(sum);

Which of the following code segments will produce the same output as the code segment above?

Page 6 of 14 AP Computer Science A


Test Booklet

Unit 6 Topic Questions

int sum = 0;
for (int k = 0; k < arr.length; k++)
{
(A) sum = sum + 2 * k;
}
System.out.print(sum);
int sum = 0;
for (int k = 0; k <= arr.length; k++)
{
(B) sum = sum + 2 * k;
}
System.out.print(sum);
int sum = 0;
for (int k = 1; k <= arr.length; k++)
{
(C) sum = sum + 2 * k;
}
System.out.print(sum);
int sum = 0;
for (int k = 0; k < arr.length; k++)
{
(D) sum = sum + 2 * arr[k];
}
System.out.print(sum);
int sum = arr[0];
for (int k = 1; k <= arr.length; k++)
{
(E) sum = sum + 2 * arr[k];
}
System.out.print(sum);

8. The code segment below is intended to print the length of the shortest string in the array wordArray. Assume
that wordArray contains at least one element.

int shortest = /* missing value */;


for (String word : wordArray)
{
if (word.length() < shortest)
{
shortest = word.length();
}
}
System.out.println(shortest);

Which of the following should be used as the initial value assigned to shortest so that the code segment works
as intended?

AP Computer Science A Page 7 of 14


Test Booklet

Unit 6 Topic Questions

(A) Integer.MAX_VALUE
(B) Integer.MIN_VALUE
(C) 0
(D) word.length()
(E) wordArray.length

9. 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;
}

Which of the following code segments can be used to replace /* missing code */ so that diff will work as
intended?
if (pred[i] < act[i])
{
(A) num = act[i] - pred[i];
}
if (pred[i] > act[i])
{
(B) num = pred[i] - act[i];
}
if (pred[i] - act[i] > num)
{
(C) num = pred[i] - act[i];
}
if (Math.abs(pred[i] - act[i]) < num)
{
(D) num = Math.abs(pred[i] - act[i]);
}
if (Math.abs(pred[i] - act[i]) > num)
{
(E) num = Math.abs(pred[i] - act[i]);
}

Page 8 of 14 AP Computer Science A


Test Booklet

Unit 6 Topic Questions

10. 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;
(B) int longest = words[0].length();
(C) String longest = "";
(D) String longest = words[0];
(E) String longest = words[1];

11. Consider the following code segment.

int[] arr = {1, 2, 3, 4, 5, 6, 7};


for (int i = 1; i < arr.length; i += 2)
{
arr[i] = arr[i - 1];
}

Which of the following represents the contents of the array arr after the code segment is executed?
(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}

AP Computer Science A Page 9 of 14


Test Booklet

Unit 6 Topic Questions

12. Consider the following method.

public int[] addNum(int[] array, int first, int second, int num)
{
int[] newArray = new int[array.length];
newArray[first] = array[first] + num;
newArray[second] = array[second] + num;
return newArray;
}

Which of the following code segments, appearing in the same class as the addNum method, will result in
array2 having the contents {0, 0, 13, 0, 9, 0, 0} ?
int[] array1 = {5, 2, 8, 6, 4, 3, 9};
(A) int[] array2 = addNum(array1, 2, 4, 5);

int[] array1 = {-5, -5, 13, 0, 9, 0, 0};


(B) int[] array2 = addNum(array1, 2, 4, 5);
int[] array1 = {5, 2, 8, 6, 4, 3, 9};
(C) int[] array2 = addNum(array1, 3, 5, 5);
int[] array1 = {5, 8, 2, 4, 6, 3, 9};
(D) int[] array2 = addNum(array1, 2, 4, 5);

int[] array1 = {0, -5, 8, 0, 9, 0, 0};


(E) int[] array2 = addNum(array1, 2, 4, 5);

13. 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, if any, 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.

Page 10 of 14 AP Computer Science A


Test Booklet

Unit 6 Topic Questions

14. 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 ?
int arr = {40, 30, 20, 10, 0};
(A) System.out.println(getValue(arr, 1, 2));

int[] arr = {40, 30, 20, 10, 0};


(B) System.out.println(getValue(arr, 1, 2));
int[] arr = {50, 40, 30, 20, 10};
(C) System.out.println(getValue(arr, 1, 2));
int arr = {40, 30, 20, 10, 0};
(D) System.out.println(getValue(arr, 2, 1));

int arr = {50, 40, 30, 20, 10};


(E) System.out.println(getValue(arr, 2, 1));

15. 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];
(B) newArray[k] = arr[k - arr.length];
(C) newArray[k] = arr[k - arr.length - 1];
(D) newArray[k] = arr[arr.length - k];
(E) newArray[k] = arr[arr.length - k - 1];

AP Computer Science A Page 11 of 14


Test Booklet

Unit 6 Topic Questions

16. Consider the following code segment, which traverses two integer arrays of equal length. If any element of arr1
is smaller than the corresponding (i.e., at the same index) element of minArray, the code segment should replace
the element of minArray with the corresponding element of arr1. After the code segment executes,
minArray should hold the smaller of the two elements originally found at the same indices in arr1 and
minArray and arr1 should remain unchanged.

for (int c = 0; c < arr1.length; c++)


{
if (arr1[c] < minArray[c])
{
arr1[c] = minArray[c];
}
else
{
minArray[c] = arr1[c];
}
}

Which of the following changes will ensure that the code segment always works as intended?
(A) Changing the Boolean expression in line 1 to c <= arr1.length
(B) Changing the relational operator in line 3 to >
(C) Removing lines 5–8
(D) Swapping the positions of line 5 and line 9
(E) Removing lines 7–10

17. Consider the following code segment.

boolean[] oldVals = {true, false, true, true};


boolean[] newVals = new boolean[4];
for (int j = oldVals.length - 1; j >= 0; j--)
{
newVals[j] = !(oldVals[j]);
}

What, if anything, will be the contents of newVals as a result of executing the code segment?
(A) {true, true, false, true}
(B) {true, false, true, true}
(C) {false, true, false, false}
(D) {false, false, true, false}
(E) The array newVals will not contain any values because the code segment does not compile.

Page 12 of 14 AP Computer Science A


Test Booklet

Unit 6 Topic Questions

18. Consider the following code segment.

int[] arr = {1, 2, 4, 0, 3};


for (int i : arr)
{
System.out.print(i);
}

Which of the following code segments will produce the same output as the code segment above?

I.
int[] arr = {1, 2, 4, 0, 3};
for (int i : arr)
{
System.out.print(arr[i]);
}

II.
int[] arr = {1, 2, 4, 0, 3};
for (int i = 0; i < arr.length; i++)
{
System.out.print(i);
}

III.
int[] arr = {1, 2, 4, 0, 3};
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i]);
}
(A) I only
(B) III only
(C) I and II only
(D) I and III only
(E) I, II, and III

AP Computer Science A Page 13 of 14


Test Booklet

Unit 6 Topic Questions

19. The twoInARow method below is intended to return true if any two consecutive elements of the parameter
arr are equal in value and return false otherwise.

public boolean twoInARow(int[] arr)


{
/* missing loop header */
{
if (arr[k] == arr[k + 1])
{
return true;
}
}
return false;
}

Which of the following can be used to replace /* missing loop header */ so that the method will work as
intended?
(A) for (int k = 0; k < arr.length - 1; k++)
(B) for (int k = 0; k < arr.length; k++)
(C) for (int k = 1; k < arr.length; k++)
(D) for (int k = arr.length - 1; k >= 0; k--)
(E) for (int k = arr.length - 1; k > 0; k--)

20. Consider the following code segment.

int[] arr = {3, 1, 0, 4, 2};


for(int j = 0; j < arr.length; j++)
{
System.out.print(arr[j] + j + " ");
}

What, if anything, is printed as a result of executing the code segment?


(A) 3 1 0 4 2
(B) 3 2 2 7 6
(C) 6 2 0 8 4
(D) 7 2 3 6 2
(E) Nothing is printed, because an ArrayIndexOutOfBoundsException is thrown.

Page 14 of 14 AP Computer Science A

You might also like