NEW
Font size
WorksheetsCSA unit 6 test review
Total questions: 80
Worksheet time: 40mins
Consider the following code segment.
int[] arr = {1, 2, 3, 4, 5};
Which of the following code segments would correctly set the first two elements of array arr to 10 so that the new value of array arr will be {10, 10, 3, 4, 5} ?
arr[0] = 10;
arr[1] = 10;
arr[1] = 10;
arr[2] = 10;
arr[0, 1] = 10;
arr[1, 2] = 10;
arr = 10, 10, 3, 4, 5;
Consider the following method.
public int[] transform(int[] a)
{
a[0]++;
a[2]++;
return a;
}
The following code segment appears in a method in the same class as transform.
/ missing code /
arr = transform(arr);
After executing the code segment, the array arr should contain {1, 0, 1, 0}. Which of the following can be used to replace / missing code / so that the code segment works as intended?
int[] arr = {0, 0, 0, 0};
int[] arr = new int[0];
int[] arr = new int[4];
I only
II only
III only
I and II
I and III
Consider the following method. Method allEven is intended to return true if all elements in array arr are even numbers; otherwise, it should return false.
public boolean allEven(int[] arr)
{
boolean isEven = /* expression */ ;
for (int k = 0; k < arr.length; k++)
{
/* loop body */
}
return isEven;
}
Which of the following replacements for /* expression / and / loop body */ should be used so that method allEven will work as intended?
/* expression // loop body */
false -----
if ((arr[k] % 2) == 0)
isEven = true;
/* expression // loop body */
false-----
if ((arr[k] % 2) != 0)
isEven = false;
else
isEven = true;
/* expression // loop body */
true-----
if ((arr[k] % 2) != 0)
isEven = false;
/* expression // loop body */
true-----
if ((arr[k] % 2) != 0)
isEven = false;
else
isEven = true;
/* expression // loop body */
true-----
if ((arr[k] % 2) == 0)
isEven = false;
else
isEven = true;
Consider the following instance variable and incomplete method. The method is intended to return a string from the array words that would be last alphabetically.
private String[] words;
public String findLastWord()
{
/* missing implementation */
}
Assume that words has been initialized with one or more strings containing only lowercase letters. Which of the following code segments can be used to replace /* missing implementation */ so that findLastWord will work as intended?
int maxIndex = 0;
for (int k = 0; k < words.length; k++)
{
if (words[k].compareTo(maxIndex) > 0)
{
maxIndex = k;
}
}
return words[maxIndex];
int maxIndex = 0;
for (int k = 1; k <= words.length; k++)
{
if (words[k].compareTo(words[maxIndex]) > 0)
{
maxIndex = k;
}
}
return words[maxIndex];
int maxIndex = 0;
for (int k = 1; k < words.length; k++)
{
if (words[k].compareTo(words[maxIndex]) > 0)
{
maxIndex = k;
}
}
return maxIndex;
String maxWord = words[0];
for (int k = 1; k < words.length; k++)
{
if (words[k].compareTo(maxWord) > 0)
{
maxWord = k;
}
}
return maxWord;
String maxWord = words[0];
for (int k = 1; k < words.length; k++)
{
if (words[k].compareTo(maxWord) > 0)
{
maxWord = words[k];
}
}
return maxWord;
A BoundedintArray represents an indexed list of integers. In a BoundedIntArray the user can specify a size, in which case the indices range from 0 to size - 1. The user can also specify the lowest index, low, in which case the indices can range from low to low + size - 1.
public class BoundedIntArray
{
private int[] myItems; // storage for the list
private int myLowIndex; // lowest index
public BoundedIntArray(int size)
{
myItems = new int[size];
myLowIndex = 0;
}
public BoundedIntArray(int size, int low)
{
myItems = new int[size];
myLowIndex = low;
}
// other methods not shown
}
Consider the following statements.
BoundedIntArray arrl = new BoundedIntArray(100, 5);
BoundedIntArray arr2 = new BoundedIntArray(100);
Which of the following best describes arrl and arr2 after these statements?
arrl and arr2 both represent lists of integers indexed from 0 to 99.
arrl and arr2 both represent lists of integers indexed from 5 to 104.
arrl represents a list of integers indexed from 0 to 104, and arr2 represents a list of integers indexed from 0 to 99.
arrl represents a list of integers indexed from 5 to 99, and arr2 represents a list of integers indexed from 0 to 99.
arrl represents a list of integers indexed from 5 to 104, and arr2 represents a list of integers indexed from 0 to 99.
Consider the following method, which is intended to return the number of strings of length greater than or equal to 3 in an array of String objects.
public static int checkString(String[] arr)
{
int count = 0;
for (int k = 0; k < arr.length; k++)
{
if (arr[k].length() >= 3)
{
count++;
}
}
return count;
}
Which of the following code segments compile without error?
checkString(new String[]);
checkString(new String[0]);
String[] str = {"cat", "dog"};checkString(str);
II only
III only
I and III only
II and III only
I, II, and III
Consider the following instance variable and incomplete method. The method calcTotal is intended to return the sum of all values in vals.
private int[] vals;
public int calcTotal()
{
int total = 0;
/* missing code */
return total;
}
Which of the code segments shown below can be used to replace /* missing code */ so that calcTotal will work as intended?
I. for (int pos = 0; pos < vals.length; pos++)
{
total += vals[pos];
}
II. for (int pos = vals.length; pos > 0; pos--)
{
total += vals[pos];
}
III. int pos = 0;
while (pos < vals.length)
{
total += vals[pos];
pos++;
}
I only
II only
III only
I and III
II and III
Consider the following two methods that appear within a single class.
public void changeIt(int[] list, int num)
{
list = new int[5];
num = 0;
for (int x = 0; x < list.length; x++)
list[x] = 0;
}
public void start()
{
int[] nums = {1, 2, 3, 4, 5};
int value = 6;
changeIt(nums, value);
for (int k = 0; k < nums.length; k++)
System.out.print(nums[k] + " ");
System.out.print(value);
}
What is printed as a result of the call start()?
0 0 0 0 0 0
0 0 0 0 0 6
1 2 3 4 5 6
1 2 3 4 5 0
The following incomplete method is intended to return the largest integer in the array numbers.
// precondition: numbers.length > 0
public static int findMax(int[]numbers)
{
int posOfMax = O;
for (int index = 1; index < numbers.length; index++)
{
if ( /*condition*/ )
{
/* statement */
}
}
return numbers[posOfMax];
}
Which of the following can be used to replace /* condition / and / statement */ so that findMax will work as intended?
/* condition / / statement */
numbers[index] > numbers[posOfMax]
posOfMax = numbers[index];
/* condition / / statement */
numbers[index] > numbers[posOfMax]
posOfMax = index;
/* condition / / statement */
numbers[index] > posOfMax
posOfMax = numbers[index];
/* condition / / statement */
numbers[index] < posOfMax
posOfMax = numbers[index];
/* condition / / statement */
numbers[index] < numbers[posOfMax]
posOfMax = index;
The following question refer to the following information.
Consider the following data field and method. Method maxHelper is intended to return the largest value among the first numVals values in an array; however, maxHelper does not work as intended.
private int[] nums;
// precondition: 0 < numVals <= nums.length
private int maxHelper(int numVals)
{
Line 1: int max = maxHelper(numVals - 1);
Line 2: if (max > nums[numVals - 1])
return max;
else
return nums[numVals - 1];
}
Which of the following corrects the method maxHelper so that it works as intended?
Insert the following statement before Line 1.
if (numVals == 0)
return numVals;
Insert the following statement before Line 1.
if (numVals == 1
return nums[0];
Insert the following statement between Line 1 and Line 2.
if (numVals == 0)
return numVals;
Insert the following statement between Line 1 and Line 2.
if (numVals == 1)
return nums[0];
Insert the following statement between Line 1 and Line 2.
if (numVals < 2)
return numVals;
Consider the following method.
public static int mystery(int value)
{
int sum = 0;
int[] arr = {1, 4, 2, 5, 10, 3, 6, 4};
for (int item : arr)
{
if (item > value)
{
sum += item;
}
}
return sum;
}
What value is returned as a result of the call mystery(4) ?
6
15
21
29
35
Consider the following method.
public static int mystery(int[] arr)
{
int count = 0;
int curr = arr[arr.length - 1];
for (int value : arr)
{
if (value > curr)
{
count = count + 1;
}
else
{
count = count – 1;
}
curr = value;
}
return count;
}
The following code segment appears in another method of the same class.
int[] arr = {4, 14, 15, 3, 14, 18, 19};
System.out.println(mystery(arr));
What is printed as a result of executing the code segment?
-7
-6
3
5
7
Consider the following code segment.
int[] numbers = new int[5];
numbers[0] = 2;
numbers[1] = numbers[0] + 1;
numbers[numbers[0]] = numbers[1];
for (int x = 3; x < numbers.length; x++)
{
numbers[x] = numbers[x - 1] * 2;
}
Which of the following represents the contents of the array numbers after the code segment is executed?
{2, 3, 0, 0, 0}
{2, 3, 1, 2, 4}
{2, 3, 3, 6, 9}
{2, 3, 3, 6, 12}
{2, 4, 8, 16, 32}
Consider the following code segment.
int[] arr = {10, 20, 30, 40, 50};
for(int x = 1; x < arr.length - 1; x++)
{
arr[x + 1] = arr[x] + arr[x + 1];
}
Which of the following represents the contents of arr after the code segment has been executed?
{10, 20, 30, 70, 120}
{10, 20, 50, 90, 50}
{10, 20, 50, 90, 140}
{10, 30, 60, 100, 50}
{10, 30, 60, 100, 150}
Consider the following code segment.
int[] arr = {4, 3, 2, 1, 0};
int total = 0;
for (int k = 0; k <= total; k++)
{
if (arr[k] % 2 == 0)
{
total += arr[k];
}
else
{
total -= arr[k];
}
}
System.out.print(total);
What, if anything, is printed as a result of executing the code segment?
2
1
0
-4
Nothing is printed because the code segment causes a runtime error
The array fruits is declared below.
String [] fruits = {"apples", "bananas", "cherries", "dates"};
Which of the following code segments will cause an ArrayIndexOutOfBoundsException ?
I.
for (int i = 0; i <= fruits.length; i++)
{
System.out.println(fruits[i]);
}
II.
for (int i = 0; i <= fruits.length - 1; i++)
{
System.out.println(fruits[i]);
}
III.
for (int i = 1; i <= fruits.length; i++)
{
System.out.println(fruits[i - 1]);
}
I only
II only
I and III only
II and III only
I, II, and III
The Fibonacci numbers are a sequence of integers. The first two numbers are 1 and 1. Each subsequent number is equal to the sum of the previous two integers. For example, the first seven Fibonacci numbers are 1, 1, 2, 3, 5, 8, and 13.
The following code segment is intended to fill the fibs array with the first ten Fibonacci numbers. The code segment does not work as intended.
int[] fibs = new int[10];
fibs[0] = 1;
fibs[1] = 1;
for (int j = 1; j < fibs.length; j++)
{
fibs[j] = fibs[j - 2] + fibs[j - 1];
}
Which of the following best identifies why the code segment does not work as intended?
In the for loop header, the initial value of j should be 0.
In the for loop header, the initial value of j should be 2.
The for loop condition should be j < fibs.length - 1.
The for loop condition should be j < fibs.length + 1.
The for loop should increment j by 2 instead of by 1.
Consider the following code segment.
int[] numbers = {1, 2, 3, 4, 5, 6};
for (int i = 0; i < numbers.length; i++)
{
System.out.println(numbers[i]);
}
Which of the following for loops produces the same output as the code segment?
for (int x : numbers)
{
System.out.println(numbers[x]);
}
for (int x : numbers)
{
System.out.println(numbers);
}
for (int x : numbers)
{
System.out.println(x);
}
for (numbers : int x)
{
System.out.println(numbers[x]);
}
for (numbers : int x)
{
System.out.println(x);
}
Consider the following two code segments.
I.
int[] arr = {1, 2, 3, 4, 5};
for (int x = 0; x < arr.length; x++)
{
System.out.print(arr[x + 3]);
}
II.
int[] arr = {1, 2, 3, 4, 5};
for (int x : arr)
{
System.out.print(x + 3);
}
Which of the following best describes the behavior of code segment I and code segment II ?
Both code segment I and code segment II will print 45.
Both code segment I and code segment II will print 45678.
Code segment I will cause an ArrayIndexOutOfBoundsException and code segment II will print 45.
Code segment I will cause an ArrayIndexOutOfBoundsException and code segment II will print 45678.
Both code segment I and code segment II will cause an ArrayIndexOutOfBoundsException.
The code segment below is intended to set the boolean variable duplicates to true if the int array arr contains any pair of duplicate elements. Assume that arr has been properly declared and initialized.
boolean duplicates = false;
for (int x = 0; x < arr.length - 1; x++)
{
/ missing loop header /
{
if (arr[x] == arr[y])
{
duplicates = true;
}
}
}
Which of the following can replace / missing loop header / so that the code segment works as intended?
for (int y = 0; y <= arr.length; y++)
for (int y = 0; y < arr.length; y++)
for (int y = x; y < arr.length; y++)
for (int y = x + 1; y < arr.length; y++)
for (int y = x + 1; y <= arr.length; y++)
Consider the following code segment, which is intended to print the maximum value in an integer array values. Assume that the array has been initialized properly and that it contains at least one element.
int maximum = / missing initial value /;
for (int k = 1; k < values.length; k++)
{
if (values[k] > maximum)
{
maximum = values[k];
}
}
System.out.println(maximum);
Which of the following should replace / missing initial value / so that the code segment will work as intended?
0
values[0]
values[1]
Integer.MIN_VALUE
Integer.MAX_VALUE
Consider the following method, which is intended to return the index of the first negative integer in a given array of integers.
public int positionOfFirstNegative(int[] values)
{
int index = 0;
while (values[index] >= 0)
{
index++;
}
return index;
}
What precondition is needed on the values array so that the method will work as intended?
The array values must contain at least one negative integer.
The array values must contain at least one nonnegative integer.
The array values must contain at least one positive integer.
No precondition is needed. The method will never work as intended.
No precondition is needed. The method will always work as intended.
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++)
{
sum = sum + 2 * k;
}
System.out.print(sum);
int sum = 0;
for (int k = 0; k <= arr.length; k++)
{
sum = sum + 2 * k;
}
System.out.print(sum);
int sum = 0;
for (int k = 1; k <= arr.length; k++)
{
sum = sum + 2 * k;
}
System.out.print(sum);
int sum = 0;
for (int k = 0; k < arr.length; k++)
{
sum = sum + 2 * arr[k];
}
System.out.print(sum);
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]);
}
I only
III only
I and II only
I and III only
What is returned from mystery when it is passed {10, 30, 30, 60}?
public static double mystery(int[] arr)
{
double output = 0;
for (int i = 0; i < arr.length; i++)
{
output = output + arr[i];
}
return output / arr.length;
}
17.5
30.0
130
32
32.5
Given the following values of a and the method doubleLast what will the values of a be after you execute: doubleLast()?
private int[ ] a = {-10, -5, 1, 4, 8, 30}; public void doubleLast()
{
for (int i = a.length / 2;
i < a.length; i++)
{
a[i] = a[i] * 2;
}
}
{-20, -10, 2, 8, 16, 60}
{-20, -10, 2, 4, 8, 30}
{-10, -5, 1, 8, 16, 60}
{-10, -5, 1, 4, 8, 30}
What are the values in a after multAll(3) executes?
private int[ ] a = {1, 3, -5, -2};
public void multAll(int amt)
{
int i = 0;
while (i < a.length)
{
a[i] = a[i] * amt; i++;
} // end while
} // end method
{1, 3, -5, -2}
{3, 9, -15, -6}
{2, 6, -10, -4}
The code will never stop executing due to an infinite loop
What are the values in a after mult(2) executes?
private int[ ] a = {1, 3, -5, -2};
public void mult(int amt)
{
int i = 0;
while (i < a.length)
{
a[i] = a[i] * amt;
} // end while
} // end method
{1, 3, -5, -2}
{3, 9, -15, -6}
{2, 6, -10, -4}
The code will never stop executing due to an infinite loop
Which of the following statements is a valid conclusion. Assume that variable b is an array of k integers and that the following is true:
b[0] != b[i] for all i from 1 to k-1
The value in b[0] does not occur anywhere else in the array
Array b is sorted
Array b is not sorted
Array b contains no duplicates
The value in b[0] is the smallest value in the array
Consider the following code segment. Which of the following statements best describes the condition when it returns true?
boolean temp = false;
for (int i = 0; i < a.length; i++)
{
temp = (a[i] == val);
}
return temp;
whenever the first element in a is equal to val
Whenever a contains any element which equals val
Whenever the last element in a is equal to val
Whenever more than 1 element in a is equal to val
Whenever exactly 1 element in a is equal to val
Consider the following data field and method findLongest. Method findLongest is intended to find the longest consecutive block of the value target occurring in the array nums; however, findLongest does not work as intended. For example given the code below the call findLongest(10) should return 3, the length of the longest consecutive block of 10s. Which of the following best describes the value actually returned by a call to findLongest?
private int[] nums = {7, 10, 10, 15, 15, 15, 15, 10, 10, 10, 15, 10, 10};
public int findLongest(int target)
{
int lenCount = 0;
// length of current consecutive numbers int maxLen = 0;
// max length of consecutive numbers
for (int k = 0; k < nums.length; k++)
{
if (nums[k] == target)
{
lenCount++;
}
else if (lenCount > maxLen)
{
maxLen = lenCount;
}
}
if (lenCount > maxLen)
{
maxLen = lenCount;
}
return maxLen;
}
It is the length of the shortest consecutive block of the value target in nums
It is the length of the array nums
. It is the length of the first consecutive block of the value target in nums
It is the number of occurrences of the value target in nums
It is the length of the last consecutive block of the value target in nums
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?
The array values must be sorted in ascending order.
The array values must be sorted in descending order.
The array values must have only one mode.
The array values must not contain values whose sum is not 0.
No precondition is necessary; the method will always work as intended.
Assume that the array arr has been defined and initialized as follows.
Which of the following will correctly print all of the odd integers contained in arr but none of the even integers contained in arr ?
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?
newArray[k] = arr[-k];
newArray[k] = arr[k - arr.length];
newArray[k] = arr[k - arr.length - 1];
newArray[k] = arr[arr.length - k];
newArray[k] = arr[arr.length - k - 1];
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};
System.out.println(getValue(arr, 1, 2));
int[] arr = {40, 30, 20, 10, 0};
System.out.println(getValue(arr, 1, 2));
int[] arr = {50, 40, 30, 20, 10};
System.out.println(getValue(arr, 1, 2));
int arr = {40, 30, 20, 10, 0};
System.out.println(getValue(arr, 2, 1));
int arr = {50, 40, 30, 20, 10};
System.out.println(getValue(arr, 2, 1));
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])
{
num = act[i] - pred[i];
}
B
if (pred[i] > act[i])
{
num = pred[i] - act[i];
}
C
if (pred[i] - act[i] > num)
{
num = pred[i] - act[i];
}
D
if (Math.abs(pred[i] - act[i]) < num)
{
num = Math.abs(pred[i] - act[i]);
}
E
if (Math.abs(pred[i] - act[i]) > num)
{
num = Math.abs(pred[i] - act[i]);
}
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?
{true, true, false, true}
{true, false, true, true}
{false, true, false, false}
{false, false, true, false}
Consider the following method that is intended to return the sum of the elements in the array key.
Which of the following statements should be used to replace / missing code / so that sumArraywill work as intended?
sum = key [ i ] ;
sum += key [i - 1] ;
sum += key [ i ] ;
sum += sum + key[i - 1] ;
sum += sum + key [ i ] ;
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 */
}
Which of the following can replace /* missing code */ so the code segment works as intended?
if (b.pages > maxPages)
{
maxPages = b.pages;
}
if (b.getPages() > maxPages)
{
maxPages = b.getPages();
}
if (Book[b].pages > maxPages)
{
maxPages = Book[b].pages;
}
if (bookArr[b].pages > maxPages)
{
maxPages = bookArr[b].pages;
}
Consider the following method, isSorted, which is intended to return true if an array of integers is sorted in nondecreasing order and to return false otherwise.
/** @param data an array of integers
* @return true if the values in the array appear in sorted (nondecreasing) order
*/
public static boolean isSorted(int[] data)
{
/ missing code /
}
Which of the following can be used to replace / missing code / so that isSorted will work as intended?(B)
I. for (int k = 0; k < data.length; k++)
{
if (data[k] > data[k + 1])
return false;
}
return true;
II. for (int k = 1; k < data.length; k++)
{
if (data[k - 1] > data[k])
return false;
}
return true;
III. for (int k = 0; k < data.length - 1; k++)
{
if (data[k] > data[k + 1])
return false;
else
return true;
}
return true;
I only
ll only
Ill only
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?
Changing int j = 0; to int j = 1;
Changing j <= arr.length; to j < arr.length;
Changing j <= arr.length; to j < arr.length - 1;
Changing j <= arr.length; to j < arr.length + 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]++;
}
I only
I and III only
II and III only
I, II, and III
None of the code segments will return an equivalent result.
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?
Integer.MAX_VALUE
Integer.MIN_VALUE
0
word.length()
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?
{0, 1, 2, 3, 4, 5, 6}
{1, 1, 1, 1, 1, 1, 1}
{1, 1, 3, 3, 5, 5, 7}
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?
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 = 0; k <= arr.length; k--).
The for loop header should be replaced with for (int k = 1; k <= arr.length - 1; k++).
The statement in the body of the for loop should be replaced with sum += arr[0].
The statement in the body of the for loop should be replaced with sum += arr[k - 1].
Consider the following data field and method. Which of the following best describes the contents of myStuff in terms of m and n after the following statement has been executed?
private int[] myStuff;
//precondition: myStuff contains // integers in no particular order
public int mystery(int num)
{
for (int k = myStuff.length - 1; k >= 0; k--)
{
if (myStuff[k] < num)
{
return k;
}
}
return -1;
}
//bottom is seperate
int m = mystery(n)
All values in positions m+1 through myStuff.length-1 are greater than or equal to n.
All values in position 0 through m are less than n.
All values in position m+1 through myStuff.length-1 are less than n.
The smallest value is at position m.E. The largest value that is smaller than n is at position m.
Consider the following field arr and method checkArray. Which of the following best describes what checkArray returns?
private int[] arr;
// precondition:
arr.length != 0 public int checkArray()
{
int loc = arr.length / 2;
for (int k = 0; k < arr.length; k++)
{
if (arr[k] > arr[loc])
{
loc = k;
}
}
return loc;
}
. Returns the index of the largest value in array arr.
Returns the index of the first element in array arr whose value is greater than arr[loc].
Returns the index of the last element in array arr whose value is greater than arr[loc].
Returns the largest value in array arr.
Returns the index of the largest value in the second half of array arr.
Given the following field and method declaration, what is the value in a[1] when m1(a) is run?
int[] a = {7, 3, -1};
public static int m1(int[] a)
{
a[1]--;
return (a[1] * 2);
}
4
2
12
6
3
Consider the following code. What is the maximum amount of times that HELLO could possibly be printed?
for (int i = 1; i < k; i++)
{
if (arr[i] < someValue)
{
System.out.print("HELLO")
}
}
k - 1
k + 1
k
1
0
Consider the following method changeArray. An array is created that contains {2, 8, 10, 9, 6} and is passed to changeArray. What are the contents of the array after the changeArray method executes?
public static void changeArray(int[] data)
{
for (int k = data.length - 1; k > 0; k--)
data[k - 1] = data[k] + data[k - 1];
}
{2, 6, 2, -1, -3}
{-23, -21, -13, -3, 6}
{10, 18, 19, 15, 6}
This method results in an IndexOutOfBounds exception.
{35, 33, 25, 15, 6}
Assume that arr1={1, 5, 3, -8, 6} and arr2={-2, -1, -5, 3, -4} what will the contents of arr1 be after copyArray finishes executing?
public static void copyArray(int[] arr1, int[] arr2) {
for (int i = arr1.length / 2; i < arr1.length; i++)
{
arr1[i] = arr2[i];
}
}
[-2, -1, -5, 3, -4]
[-2, -1, 3, -8, 6]
[1, 5, -5, 3, -4]
[1, 5, 3, -8, 6]
[1, 5, -2, -5, 2]
Given the following code segment, which of the following will cause an infinite loop? Assume that temp is an int variable initialized to be greater than zero and that a is an array of ints.
for ( int k = 0; k < a.length; k++ )
{
while ( a[ k ] < temp )
{
a[ k ] *= 2;
}
}
The values don't matter this will always cause an infinite loop.
Whenever a includes a value that is less than or equal to zero.
Whenever a has values larger then temp.
When all values in a are larger than temp.
Whenever a includes a value equal to temp.
Given the following array instance variable and method, which of the following best describes the contents of myStuff after (int m = mystery(n);) has been executed?
// private field in the class
private int[ ] myStuff;
//precondition: myStuff contains // integers in no particular order
public int mystery(int num)
{
for (int k = myStuff.length - 1; k >= 0; k--)
{
if (myStuff[k] < num)
{
return k;
}
}
return -1;
}
All values in positions m+1 through myStuff.length-1 are greater than or equal to n.
All values in position 0 through m are less than n.
All values in position m+1 through myStuff.length-1 are less than n.
The smallest value is at position m.
Consider the following class definition.
public class Toy
{
private int yearFirstSold;
public int getYearFirstSold()
{
return yearFirstSold;
}
/ There may be instance variables, constructors, and other methods not shown. /
}
The following code segment, which appears in a class other than Toy, prints the year each Toy object in toyArray was first sold by its manufacturer. Assume that toyArray is a properly declared and initialized array of Toy objects.
for (Toy k : toyArray)
{
System.out.println(k.getYearFirstSold());
}
Which of the following could be used in place of the given code segment to produce the same output?
I.
for (int k = 0; k < toyArray.length; k++)
{
System.out.println(getYearFirstSold(k));
}
II.
for (int k = 0; k < toyArray.length; k++)
{
System.out.println(k.getYearFirstSold());
}
III.
for (int k = 0; k < toyArray.length; k++)
{
System.out.println(toyArray[k].getYearFirstSold());
}
I only
II only
III only
I and II
II and III
In the code segment below, assume that the int array numArr has been properly declared and initialized. The code segment is intended to reverse the order of the elements in numArr. For example, if numArr initially contains {1, 3, 5, 7, 9}, it should contain {9, 7, 5, 3, 1} after the code segment executes.
/ missing loop header /
{
int temp = numArr[k];
numArr[k] = numArr[numArr.length - k - 1];
numArr[numArr.length - k - 1] = temp;
}
Which of the following can be used to replace / missing loop header / so that the code segment works as intended?
for (int k = 0; k < numArr.length / 2; k++)
for (int k = 0; k < numArr.length; k++)
for (int k = 0; k < numArr.length / 2; k--)
for (int k = numArr.length - 1; k >= 0; k--)
for (int k = numArr.length - 1; k >= 0; k++)
public class TimeRecord
{
private int hours;
private int minutes; // 0 < minutes < 60
/** Constructs a TimeRecord object.
* @param h the number of hours
* Precondition: h > 0
* @param m the number of minutes
* Precondition: 0 < m < 60
*/
public TimeRecord(int h, int m)
{
hours = h;
minutes = m;
}
/** @return the number of hours
*/
public int getHours()
{ / implementation not shown / }
/** @return the number of minutes
* Postcondition: 0 < minutes < 60
*/
public int getMinutes()
{ / implementation not shown / }
/**
Adds h hours and m minutes to this TimeRecord.
* @param h the number of hours
* Precondition: h > 0
* @param m the number of minutes
* Precondition: m > 0
*/
public void advance(int h, int m)
{
hours = hours + h;
minutes = minutes + m;
/ missing code /
}
// Other methods not shown
}
Consider the following declaration that appears in a class other than TimeRecord.
TimeRecord[] timeCards = new TimeRecord[100];
Assume that timeCards has been initialized with TimeRecord objects. Consider the following code segment that is intended to compute the total of all the times stored in timeCards.
TimeRecord total = new TimeRecord(0,0);
for (int k = 0; k < timeCards.length; k++)
{
/ missing expression / ;
}
Which of the following can be used to replace / missing expression / so that the code segment will work as intended?
timeCards[k].advance()
total += timeCards[k].advance()
total.advance(timeCards[k].hours,
timeCards[k].minutes)
total.advance(timeCards[k].getHours(),
timeCards[k].getMinutes())
timeCards[k].advance(timeCards[k].getHours(),
timeCards[k].getMinutes())
Consider the following instance variable and method.
private int[] arr;
/** Precondition: arr contains no duplicates;
* the elements in arr are in ascending order.
* @param low an int value such that 0 < low < arr.length
* @param high an int value such that low - 1 < high < arr.length
* @param num an int value
*/
public int mystery(int low, int high, int num)
{
int mid = (low + high) / 2;
if (low > high)
{
return low;
}
else if (arr[mid] < num)
{
return mystery(mid + 1, high, num);
}
else if (arr[mid] > num)
{
return mystery(low, mid − 1, num);
}
else // arr[mid] == num
{
return mid;
}
}
What is returned by the call mystery(0, arr.length − 1, num)?
The number of elements in arr that are less than num
The number of elements in arr that are less than or equal to num
The number of elements in arr that are equal to num
The number of elements in arr that are greater than num
The index of the middle element in arr
Consider the following instance variable nums and method findLongest with line numbers added for reference. Method findLongest is intended to find the longest consecutive block of the value target occurring in the array nums; however, findLongest does not work as intended.
For example, if the array nums contains the values [7, 10, 10, 15, 15, 15, 15, 10, 10, 10, 15, 10, 10], the call findLongest(10) should return 3, the length of the longest consecutive block of 10s.
private int[] nums;
public int findLongest(int target)
{
int lenCount = 0;
int maxLen = 0;
Line 1: for (int val : nums)
Line 2: {
Line 3: if (val == target)
Line 4: {
Line 5: lenCount++;
Line 6: }
Line 7: else
Line 8: {
Line 9: if (lenCount > maxLen)
Line 10: {
Line 11: maxLen = lenCount;
Line 12: }
Line 13: }
Line 14: }
Line 15: if (lenCount > maxLen)
Line 16: {
Line 17: maxLen = lenCount;
Line 18: }
Line 19: return maxLen;
}
The method findLongest does not work as intended. Which of the following best describes the value returned by a call to findLongest?
It is the length of the shortest consecutive block of the value target in nums.
It is the length of the array nums.
It is the number of occurrences of the value target in nums.
It is the length of the first consecutive block of the value target in nums.
It is the length of the last consecutive block of the value target in nums.
Consider the following instance variable nums and method findLongest with line numbers added for reference. Method findLongest is intended to find the longest consecutive block of the value target occurring in the array nums; however, findLongest does not work as intended.
For example, if the array nums contains the values [7, 10, 10, 15, 15, 15, 15, 10, 10, 10, 15, 10, 10], the call findLongest(10) should return 3, the length of the longest consecutive block of 10s.
private int[] nums;
public int findLongest(int target)
{
int lenCount = 0;
int maxLen = 0;
Line 1: for (int val : nums)
Line 2: {
Line 3: if (val == target)
Line 4: {
Line 5: lenCount++;
Line 6: }
Line 7: else
Line 8: {
Line 9: if (lenCount > maxLen)
Line 10: {
Line 11: maxLen = lenCount;
Line 12: }
Line 13: }
Line 14: }
Line 15: if (lenCount > maxLen)
Line 16: {
Line 17: maxLen = lenCount;
Line 18: }
Line 19: return maxLen;
}
Which of the following changes should be made so that methodfindLongest will work as intended?
Insert the statement lenCount = 0; between lines 2 and 3.
Insert the statement lenCount = 0; between lines 8 and 9.
Insert the statement lenCount = 0; between lines 10 and 11.
Insert the statement lenCount = 0; between lines 11 and 12.
Insert the statement lenCount = 0; between lines 12 and 13.
Consider the following sort method. This method correctly sorts the elements of array data into increasing order.
Assume that sort is called with the array {6, 3, 2, 5, 4, 1}. What will the value of data be after three passes of the outer loop (i.e., when j = 2 at the point indicated by / End of outer loop /) ?
{1, 2, 3, 4, 5, 6}
{1, 2, 3, 5, 4, 6}
{1, 2, 3, 6, 5, 4}
{1, 3, 2, 4, 5, 6}
{1, 3, 2, 5, 4, 6}
Consider the following instance variable and method.
private int[] numbers;
/* Precondition: numbers contains int values in no particular order. /
public int mystery(int num)
{
for (int k = numbers.length − 1; k >= 0; k−−)
{
if (numbers[k] < num)
{
return k;
}
}
return -1;
}
Which of the following best describes the contents of numbers after the following statement has been executed?
int m = mystery(n);
All values in positions 0 through m are less than n.
All values in positions m+1 through numbers.length-1 are less than n.
All values in positions m+1 through numbers.length-1 are greater than or equal to n.
The smallest value is at position m.
The largest value that is smaller than n is at position m.
Consider the following instance variable and method.
private int[] array;
/* Precondition: array.length > 0 /
public int checkArray()
{
int loc = array.length / 2;
for (int k = 0; k < array.length; k++)
{
if (array[k] > array[loc])
{
loc = k;
}
}
return loc;
}
Which of the following is the best postcondition for checkArray?
Returns the index of the first element in array array whose value is greater than array[loc]
Returns the index of the last element in array array whose value is greater than array[loc]
Returns the largest value in array array
Returns the index of the largest value in array array
Returns the index of the largest value in the second half of array array
Consider the following method.
/* Precondition: arr contains only positive values. /
public static void doSome(int[] arr, int lim)
{
int v = 0;
int k = 0;
while (k < arr.length && arr[k] < lim)
{
if (arr[k] > v)
{
v = arr[k]; / Statement S /
}
k++; / Statement T /
}
}
Assume that doSome is called and executes without error. Which of the following are possible combinations for the value of lim, the number of times Statement S is executed, and the number of times Statement T is executed?
lim: S: T:
I. 5 0 5
II. 7 4 9
III. 3 5 2
I only
II only
III only
I and III only
II and III only
Consider the mode method, which is intended to return the most frequently occurring value (mode) in its int[] parameter arr. For example, if the parameter of the mode method has the contents {6, 5, 1, 5, 2, 6, 5}, then the method is intended to return 5.
/** Precondition: arr.length >= 1 */
public static int mode(int[] arr)
{
int modeCount = 1;
int mode = arr[0];
for (int j = 0; j < arr.length; j++)
{
int valCount = 0;
for (int k = 0; k < arr.length; k++)
{
if ( /* missing condition 1 */ )
{
valCount++;
}
}
if ( /* missing condition 2 */ )
{
modeCount = valCount;
mode = arr[j];
}
}
return mode;
}
Which of the following can replace /* missing condition 1 / and / missing condition 2 */ so the code segment works as intended?
/* missing condition 1 // missing condition 2 */
arr[j] == arr[k], valCount > modeCount
/* missing condition 1 // missing condition 2 */
arr[j] == arr[k], modeCount > valCount
/* missing condition 1 // missing condition 2 */
arr[j] != arr[k], valCount > modeCount
/* missing condition 1 // missing condition 2 */
arr[j] != arr[k], modeCount > valCount
/* missing condition 1 // missing condition 2 */
arr[j] != arr[k], modeCount != valCount
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[] testOne = {"first", "day", "of", "spring"};
String[] resultOne = strArrMethod(testOne);
What are the contents of resultOne when the code segment has been executed?
{"day", "first", "of", "spring"}
{"of", "day", "first", "spring"}
{"of", "day", "of", "spring"}
{"of", "of", "of", "spring"}
{"spring", "first", "day", "of"}
Consider the following instance variable, arr, and incomplete method, partialSum. The method is intended to return an integer array sum such that for all k, sum[k] is equal to arr[0] + arr[1] + ... + arr[k]. For instance, if arr contains the values { 1, 4, 1, 3 }, the array sum will contain the values { 1, 5, 6, 9 }.
private int[] arr;
public int[] partialSum()
{
int[] sum = new int[arr.length];
for (int j = 0; j < sum.length; j++)
{
sum[j] = 0;
}
/ missing code /
return sum;
}
The following two implementations of
/ missing code / are proposed so that partialSum will work as intended.
Implementation 1
for (int j = 0; j < arr.length; j++)
{
sum[j] = sum[j - 1] + arr[j];
}
Implementation 2
for (int j = 0; j < arr.length; j++)
{
for (int k = 0; k <= j; k++)
{
sum[j] = sum[j] + arr[k];
}
}
Which of the following statements is true?
Both implementations work as intended, but implementation 1 is faster than implementation 2.
Both implementations work as intended, but implementation 2 is faster than implementation 1.
Both implementations work as intended and are equally fast.
. Implementation 1 does not work as intended, because it will cause an ArrayIndexOutOfBoundsException.
Implementation 2 does not work as intended, because it will cause an ArrayIndexOutOfBoundsException.
Consider the following incomplete method that is intended to return an array that contains the contents of its first array parameter followed by the contents of its second array parameter.
public static int [ ] append(int [ ] a1, int [ ] a2)
{
int [ ] result = new int [a1.length + a2.length];
for (int j = 0; j < a1.length; j++)
result [j] = a1 [j];
for (int k = 0; k < a2.length; k++)
result [ /* index */ ] = a2 [k];
return result;
}
Which of the following expressions can be used to replace /* index */ so that append will work as intended?
j
k
k + a1.length - 1
k + a1.length
k + a1.length + 1
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] ?
arr[0] = 5;
arr[5] = 0;
arr[0] = arr[5];
arr[5] = arr[0];
int k = arr[5];
arr[0] = arr[5];
arr[5] = k;
int k = arr[0];
arr[0] = arr[5];
arr[5] = k;
int k = arr[5];
arr[5] = arr[0];
arr[0] = arr[5];
Consider an integer array nums, which has been properly declared and initialized with one or more values. Which of the following code segments counts the number of negative values found in nums and stores the count in counter?
I.
int counter = 0;
int i = -1;while (i <= nums.length - 2)
{
i++;
if (nums[i] < 0)
{
counter++;
}
}
II.
int counter = 0;
for (int i = 1; i < nums.length; i++)
{
if (nums[i] < 0)
{
counter++;
}
}
III.
int counter = 0;
for (int i : nums)
{
if (nums[i] < 0)
{
counter++;
}
}
I only
II only
I and II only
I and III only
I, II, and III
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]);
}
I only
III only
I and II only
I and III only
I, II, and III
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};
System.out.println(getValue(arr, 1, 2));
int[] arr = {40, 30, 20, 10, 0};
System.out.println(getValue(arr, 1, 2));
int[] arr = {50, 40, 30, 20, 10};
System.out.println(getValue(arr, 1, 2));
int arr = {40, 30, 20, 10, 0};
System.out.println(getValue(arr, 2, 1));
int arr = {50, 40, 30, 20, 10};
System.out.println(getValue(arr, 2, 1));
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?
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 = 0; k <= arr.length; k--).
The for loop header should be replaced with for (int k = 1; k <= arr.length - 1; k++).
The statement in the body of the for loop should be replaced with sum += arr[0].
The statement in the body of the for loop should be replaced with sum += arr[k - 1].
Consider the problem of finding the maximum value in an array of integers. The following code segments are proposed solutions to the problem. Assume that the variable arr has been defined as an array of int values and has been initialized with one or more values.
I.
int max = Integer.MIN_VALUE;
for (int value : arr)
{
if (max < value)
{
max = value;
}
}
II.
int max = 0;
boolean first = true;
for (int value : arr)
{
if (first)
{
max = value;
first = false
}
else if (max < value)
{
max = value;
}
}
III.
int max = arr [0];
for (int k =1; k < arr.length; k++)
{
if (max < arr [k] )
{
max = arr [k];
}
}
Which of the code segments will always correctly assign the maximum element of the array to the variable max ?
I only
II only
III only
II and III only
I, II, and III
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?
{0, 1, 2, 3, 4, 5, 6}
{1, 1, 1, 1, 1, 1, 1}
{1, 1, 3, 3, 5, 5, 7}
{1, 2, 3, 4, 5, 6, 7}
{2, 2, 4, 4, 6, 6, 7}
Consider the following code segment.
int[] arr = {7, 2, 5, 3, 0, 10};
for (int k = 0; k < arr.length - 1; k++)
{
if (arr[k] > arr[k + 1])
System.out.print(k + " " + arr[k] + " ");
}
What will be printed as a result of executing the code segment?
725330
0725510
022330
173543
0 7 2 5 3 3
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)
{
numbers[k] = numbers[k - 1] + x;
}
}
Assume that numbers has been initialized with the following values.
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)?
{17, 20, 21, 42, 45, 69, 48, 51, 39}
{17, 20, 23, 26, 29, 32, 35, 38, 41}
{17, 37, 21, 42, 18, 69, 48, 28, 39}
{20, 23, 21, 42, 45, 69, 51, 54, 39}
{20, 34, 21, 45, 15, 69, 51, 25, 39}
Consider the following incomplete method that is intended to return a string formed by concatenating elements from the parameter words. The elements to be concatenated start with startIndex and continue through the last element ofwords and should appear in reverse order in the resulting string.
/** Precondition: words.length > 0;
* startIndex >= 0
*/
public static String concatWords(String[] words, int startIndex)
{
String result = "";
/ missing code /
return result;
}
For example, the following code segment uses a call to the concatWords method.
String[] things = {"Bear", "Apple", "Gorilla", "House", "Car"}; System.out.println(concatWords(things, 2));
When the code segment is executed, the string "CarHouseGorilla" is printed.
The following three code segments have been proposed as replacements for / missing code /.
I. for (int k = startIndex; k < words.length; k++)
{
result += words[k] + words[words.length - k - 1];
}
II. int k = words.length - 1;
while (k >= startIndex)
{
result += words[k];
k--;
}
III. String[] temp = new String[words.length];
for (int k = 0; k <= words.length / 2; k++)
{
temp[k] = words[words.length - k - 1];
temp[words.length - k - 1] = words[k];
}
for (int k = 0; k < temp.length - startIndex; k++)
{
result += temp[k];
}
Which of these code segments can be used to replace / missing code / so that concatWords will work as intended?
I only
II only
III only
I and II
II and III
Consider the following method.
public static int mystery(int[] arr)
{
int x = 0
for (int k = 0; k < arr.length; k = k + 2)
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};
5
6
7
10
17
Consider the following method.
/* Precondition: arr.length > 0 /
public static int mystery(int[] arr)
{
int index = 0;
int count = 0;
int m = -1;
for (int outer = 0; outer < arr.length; outer++)
{
count = 0;
for (int inner = outer + 1; inner < arr.length; inner++)
{
if (arr[outer] == arr[inner])
{
count++;
}
}
if (count > m)
{
index = outer;
m = count;
}
}
return index;
}
Assume that nums has been declared and initialized as an array of integer values. Which of the following best describes the value returned by the call mystery(nums)?
The maximum value that occurs in nums
An index of the maximum value that occurs in nums
The number of times that the maximum value occurs in nums
A value that occurs most often in nums
An index of a value that occurs most often in nums
segment from an insertion sort program.
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 / ) ?
{2, 3, 4, 5, 1}
{3, 2, 1, 4, 5}
{3, 4, 5, 2, 1}
{3, 5, 2, 3, 1}
{5, 3, 4, 2, 1}
