0:00

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

b[0]与数组之后的其他元素均不相等，意味着其在数组的其他地方不出现。
1

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;
temp值在循环的过程中会不断被覆盖，最终返回的是对数组末尾元素的比较。
3

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;
}
method中没有判断连续的部分，因此只是单纯的计数target出现了多少次。
4

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

int m = mystery(n)
mystery从后往前循环，在数组元素小于参数时返回索引。因而之前索引所对应的元素均是大于等于参数的。
1

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

1

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);
}
a[1]本来的值为3--后变成2，注意return语句并不改变a[1]值。
2

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")
}
}

1

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];
}

5

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];
}
}

3

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 0 and that a is an array of integers.

for ( int k = 0; k < a.length; k++ )
{
while ( a[k] < temp )
{
a[k] *= 2;
}
}
a为负数时a[k] *= 2永远小于0
2