Unit 6 Topic Questions - Array
Unit 6 Topic Questions - Array
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.
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.
/** 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) */
}
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.
theVocab:
wordArray:
"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.
*/
public String[] notInVocab(String[] wordArray)
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);
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.
Which of the following changes, if any, can be made to line 4 so that the method will work as intended?
6. Consider the following code segment, which is intended to print the sum of all elements of an array.
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?
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.
Which of the following should be used as the initial value assigned to shortest so that the code segment works
as intended?
(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]);
}
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.
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];
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}
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);
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.
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.
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));
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.
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];
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.
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
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.
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
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.
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--)