AP
CAMP
香港中文大學校友會AP課程訓練營
More Links
我们的教学
02/ 真题分析范例:计算机科学A(数学及计算机科学类)

因内容丰富,无法一一列举,我们主要介绍的是真题分析部分。

你可以从左边的列表中选择感兴趣的科目查看其真题分析范例。


CASE1

综合类考察题

【官方样题】

考试说明 第21题

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?
(A) Both implementations work as intended, but implementation 1 is faster than implementation 2.
(B) Both implementations work as intended, but implementation 2 is faster than implementation 1.
(C) Both implementations work as intended and are equally fast.
(D) Implementation 1 does not work as intended, because it will cause an ArrayIndexOutOfBoundsException.
(E) Implementation 2 does not work as intended, because it will cause an ArrayIndexOutOfBoundsException.

【中文翻译】

考虑下列实例变量及未完成的方法,这个方法要求返回一个数组的和。比如,数组arr={1,4,1,3},则他的数组的和sum={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;
}

下列两种方法可以实现上述目的:

方法1
for (int j = 0; j < arr.length; j++)
{
sum[j] = sum[j - 1] + arr[j];
}
方法2
for (int j = 0; j < arr.length; j++)
{
for (int k = 0; k <= j; k++)
{
sum[j] = sum[j] + arr[k];
}
}

下列哪种说法正确?
(A) 两个方法都能实现目标,方法一比方法二快
(B) 两个方法都能实现目标,方法二比方法一快
(C) 两个方法都能实现目标且一样快
(D) 方法一可以实现,但是会导致数组下标越界异常
(E) 方法二可以实现,但是会导致数组下标越界异常

正确答案:(D)



CASE2

综合类考察题

【官方样题】

考试说明 第23题

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[0] … 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?
(A) Executing shuffle may cause an ArrayIndexOutOfBoundsException.
(B) The first element of the returned array (result[0]) may not have the correct value.
(C) The last element of the returned array (result[result.length − 1]) may not have the correct value.
(D) One or more of nums[0] … nums[nums.length / 2 − 1] may have been copied to the wrong position(s) in the returned array.
(E) One or more of nums[nums.length / 2] … nums[nums.length − 1] may have been copied to the wrong position(s) in the returned array.

【中文翻译】

考虑一种洗牌算法,目的是要返回新的数组,其中包含所有元素的值,但顺序不同。让n代表元素的数目。洗牌算法将元素从nums[0] … nums[n/2–1]替换为nums[n / 2] … nums[n – 1],如下所示:

下列洗牌算法不能成功运行

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

下列哪个选项最好地描述了洗牌算法运行中出现的问题?
(A) 执行洗牌算法会导致数组下标越界异常
(B) 返回数组(result[0])的第一个元素的值可能不正确
(C) 返回数组(result[0])的最后一个元素的值可能不正确
(D) nums[0] … nums[nums.length / 2 − 1]中的一个多个被复制回数组时位置可能不正确
(E) nums[nums.length / 2] … nums[nums.length − 1] 中的一个多个被复制回数组时位置可能不正确

正确答案:(C)



CASE3

知识类考察题

【官方样题】

考试说明 第25题

The following sort method correctly sorts the integers in elements into ascending order.
Line 1: public static void sort(int[] elements)
Line 2: {
Line 3:   for (int j = 0; j < elements.length - 1; j++)
Line 4:   {
Line 5.     int index = j;
Line 6:
Line 7:     for (int k = j + 1; k < elements.length; k++)
Line 8:     {
Line 9:       if (elements[k] < elements[index])
Line 10:      {
Line 11:        index = k;
Line 12:       }
Line 13:     }
Line 14:
Line 15:     int temp = elements[j];
Line 16:     elements[j] = elements[index];
Line 17:     elements[index] = temp;
Line 18:   }
Line 19: }

Which of the following changes to the sort method would correctly sort the integers in elements into descending order?

I. Replace line 9 with:
Line 9:        if (elements[k] > elements[index])

II. Replace lines 15–17 with:
Line 15:       int temp = elements[index];
Line 16:       elements[index] = elements[j];
Line 17:       elements[j] = temp;

III. Replace line 3 with:
Line 3:     for (int j = elements.length − 1; j > 0; j−−)
and replace line 7 with:
Line 7:        for (int k = 0; k < j; k++)
(A) I only
(B) II only
(C) I and II only
(D) I and III only
(E) I, II, and III

【中文翻译】

下面的排序方法把元素中的整数进行了升序排列。
Line 1: public static void sort(int[] elements)
Line 2: {
Line 3:   for (int j = 0; j < elements.length - 1; j++)
Line 4:   {
Line 5.     int index = j;
Line 6:
Line 7:     for (int k = j + 1; k < elements.length; k++)
Line 8:     {
Line 9:       if (elements[k] < elements[index])
Line 10:      {
Line 11:        index = k;
Line 12:       }
Line 13:     }
Line 14:
Line 15:     int temp = elements[j];
Line 16:     elements[j] = elements[index];
Line 17:     elements[index] = temp;
Line 18:   }
Line 19: }

下列哪种排序算法能够正确地将元素中的整数进行降序排列?

I. Replace line 9 with:
Line 9:        if (elements[k] > elements[index])

II. Replace lines 15–17 with:
Line 15:       int temp = elements[index];
Line 16:       elements[index] = elements[j];
Line 17:       elements[j] = temp;

III. Replace line 3 with:
Line 3:     for (int j = elements.length − 1; j > 0; j−−)
and replace line 7 with:
Line 7:        for (int k = 0; k < j; k++)

(A) I only
(B) II only
(C) I and II only
(D) I and III only
(E) I, II, and III

正确答案:(D)



CUHK ALUMNI ASSOCIATION AP TRAINING CAMP IS A MEMBER OF CUHK ALUMNI ASSOCIATION EDUCATIONAL SCIENTIFIC AND CULTURAL FOUNDATION