0:00

Consider the following code segment.

for (int k = 0; k < 20; k = k + 2){
if (k % 3 == 1)
{
System.out.print(k + "  ");
}
}


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

k必须为偶数，且k除3余1。
2

Consider the following code segment.

List animals = new ArrayList();

animals.set(2, "lizard");
animals.remove(3);
System.out.println(animals);

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

snake替换成lizard，cat前插入fish，再移除最后的lizard。注意index从0开始。
1

Consider the following method.

public static void mystery(List<Integer> nums) {
for (int k = 0; k < nums.size(); k++) {
if (nums.get(k).intValue() == 0) {
nums.remove(k);
}
}
}

Assume that a List values initially contains the following Integer values
[0, 0, 4, 2, 5, 0, 3, 0]
What will values contain as a result of executing mystery(values)?

4

At a certain high school students receive letter grades based on the following scale.

93 or above A
From 84 to 92 inclusive B
From 75 to 83 inclusive C
Below 75 F

Which of the following code segments will assign the correct string to grade for a given integer score?
I.

if (score >= 93) grade = "A";
if (score >= 84 && score <= 92) grade = "B";
if (score >= 75 && score <= 83) grade = "C";
if (score < 75) grade = "F";

II.

if (score >= 93) grade = "A";
if (84 <= score <= 92) grade = "B";
if (75 <= score <= 83) grade = "C";
if (score < 75) grade = "F";

III.

if (score >= 93) grade = "A";
else if (score >= 84) grade = "B";
else if (score >= 75) grade = "C";
else grade = "F";

4

Consider the following output.
1 1 1 1 1 2 2 2 2 3 3 3 4 4 5
Which of the following code segments will produce this output?

4

A car dealership needs a program to store information about the cars for sale. For each car, they want to keep track of the following information: number of doors (2 or 4), whether the car has air conditioning, and its average number of miles per gallon. Which of the following is the best object-oriented program design?

1

Consider the following declarations.

public interface Shape {
int isLargerThan(Shape other);
// Other methods not shown
}
public class Circle implements Shape {
// Other methods not shown
}

Which of the following method headings of isLargerThan can be added to the declaration of the Circle class so that it will satisfy the Shape interface?
I. public int isLargerThan(Shape other)
II. public int isLargerThan(Circle other)
III. public boolean isLargerThan(Object other)

1

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
}

Which of the following can be used to replace /* missing code */ so that advance will correctly update the time?

3

Consider the following declaration that appears in a class other than TimeRecord.
TimeRecord[] timeCards = new TimeRecord;
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?

4

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 callmystery(0, arr.length − 1, num)?

1

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 callfindLongest(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;

for (int val : nums)
{
if (val == target)
{
lenCount++;
}
else
{
if (lenCount > maxLen)
{
maxLen = lenCount;
}
}
}
if (lenCount > maxLen)
{
maxLen = lenCount;
}
return maxLen;

}
The method findLongestdoes not work as intended. Which of the following best describes the value returned by a call to findLongest?

3

Which of the following changes should be made so that method findLongest will work as intended?

5

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

3

Consider the following method.

/** @param x an int value such that x >= 0
*/
public void mystery(int x) {
System.out.print(x % 10);
if ((x / 10) != 0) {
mystery(x / 10);
}
System.out.print(x % 10);
}

Which of the following is printed as a result of the call mystery(1234)?

4

Consider the following two classes.

public class Dog {
public void act() {
System.out.print("run ");
eat();
}
public void eat() {
System.out.print("eat ");
}
}
public class UnderDog extends Dog {
public void act() {
super.act();
System.out.print("sleep ");
}
public void eat() {
super.eat();
System.out.print("bark ");
}
}

Assume that the following declaration appears in a class other than Dog.
Dog fido = new UnderDog();
What is printed as a result of the call fido.act()?

4

Consider the following recursive method.

public static int mystery(int n) {
if (n <= 1) {
return 0;
} else {
return 1 + mystery(n / 2);
}
}

Assuming that $$k$$ is a nonnegative integer and $$m= 2^k$$, what value is returned as a result of the call mystery(m) ?

2

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?

4

Consider the following methods.

public void changer(String x, int y) {
x = x + "peace";
y = y * 2;
}
public void test() {
String s = "world";
int n = 6;
changer(s, n);

/* End of method */
}

When the call test() is executed, what are the values of s and n at the point indicated by /* End of method */?

1

Consider the following code segment.

int[][] mat = new int;
for (int row = 0; row < mat.length; row++) {
for (int col = 0; col < mat.length; col++) {
if (row < col) {
mat[row][col] = 1;
} else if (row == col) {
mat[row][col] = 2;
} else {
mat[row][col] = 3;
}
}
}

What are the contents of mat after the code segment has been executed?

Java是row-major order，排除头两个。考虑到行列相等时为2，利用矩阵对角线排除后一个。第一行row为0最小，在对角线之外必为1。
4

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?

Value of lim Executions of Statement S Executions of Statement T
I. 5 0 5
II. 7 4 9
III. 3 5 2
I不可能，因为条件已经明确arr只有正数，在T执行的前提下S至少会被触发一次。III不可能，是因为S执行时T一定会被触发。
2

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 + arr + ... + 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 partialSumwill 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?

j-1当j为0时为-1，会抛出异常。
4

Consider the following declaration for a class that will be used to represent points in the xy-coordinate plane.

public class Point {
private int x; // x-coordinate of the point
private int y; // y-coordinate of the point
public Point() {
x = 0;
y = 0;
}
public Point(int a, int b) {
x = a;
y = b;
} // Other methods not shown
}

The following incomplete class declaration is intended to extend the above class so that points can be named.

public class NamedPoint extends Point {
private String name; // name of point
// Constructors go here
// Other methods not shown
}

Consider the following proposed constructors for this class.
I.

public NamedPoint() {
name = "";
}

II.

public NamedPoint(int d1, int d2, String pointName) {
x = d1;
y = d2;
name = pointName;
}

III.

public NamedPoint(int d1, int d2, String pointName) {
super(d1, d2);
name = pointName;
}

Which of these constructors would be legal for the NamedPoint class?

II不合法，因为子类无法访问父类的private成员，必须是protected或者public才能直接访问。
4

Consider a shuffle method that is intended to return a new array that contains all the elements from nums, but in a different order. Let n be the number of elements in nums. The shuffle method should alternate the elements from nums ... nums[n / 2 – 1] with the elements from nums[n / 2] ... nums[n – 1], as illustrated in the following examples. The following implementation of the shuffle method does not work as intended.

public static int[] shuffle(int[] nums) {
int n = nums.length;
int[] result = new int[n];
for (int j = 0; j < n / 2; j++) {
result[j * 2] = nums[j];
result[j * 2 + 1] = nums[j + n / 2];
}
return result;
}

Which of the following best describes the problem with the given implementation of the shuffle method?

3

Consider the following Util class, which contains two methods. The completed sum1D method returns the sum of all the elements of the 1-dimensional array a.The incomplete sum2D method is intended to return the sum of all the elements of the 2-dimensional array m.

public class Util {
/** Returns the sum of the elements of the 1-dimensional array a */
public static int sum1D(int[] a) {
/* implementation not shown */
}

/** Returns the sum of the elements of the 2-dimensional array m */
public static int sum2D(int[][] m) {
int sum = 0;

/* missing code */

return sum;
}
}

Assume that sum1D works correctly. Which of the following can replace /missing code/ so that the sum2D method works correctly?
I.

for (int k = 0; k < m.length; k++) {
sum += sum1D(m[k]);
}

II.

for (int[] row: m) {
sum += sum1D(row);
}

III.

for (int[] row: m) {
for (int v: row) {
sum += v;
}
}

5

The following sort method correctly sorts the integers in elements into ascending order.

public static void sort(int[] elements)
{
for (int j = 0; j < elements.length - 1; j++)
{
int index = j;

for (int k = j + 1; k < elements.length; k++)
{
if (elements[k] < elements[index])
{
index = k;
}
}

int temp = elements[j];
elements[j] = elements[index];
elements[index] = temp;
}
}

Which of the following changes to the sort method would correctly sort the integers in elements into descending order?
I. Replace line 9 with:

if (elements[k] > elements[index])

II. Replace lines 15-17 with:

int temp = elements[index];
elements[index] = elements[j];
elements[j] = temp;

III. Replace line 3 with:

for (int j = elements.length - 1; j > 0; j--)

and replace line 7 with:

for (int k = 0; k < j; k++)

4