APCS MCQ葵花宝
APCS MCQ葵花宝
APCS MCQ葵花宝
一.Java 除法,强制转换等
1. Java 除法:
int / int => int (去掉小数部分)
double / int = > double(正常运算)
int / double = > double(正常运算)
double / double => double(正常运算)
注意:在 java 中只要涉及 double 的运算,由于小数位数有限,就会出现 round-off error(四舍五入差值)
A && true = A
A || false = A
例:6. Assume that a, b, c, and d have been declared and initialized with int values.
!((a >= b) && !(c < d))
Which of the following is equivalent to the expression above?
(A) (a < b) || (c < d)
(B) (a < b) || (c >= d)
(C) (a < b) && (c < d)
(D) (a >= b) || (c < d)
(E) (a >= b) && (c < d)
答案:A 此题要仔细分清楚大括号的范围 !((a >= b) && !(c < d)) 相当于 !(A && B)其中 A 为 (a >=
b) , B 为 !(c < d) 拆分后 !(A && B) => !A||!B, !A 为 a<b, !B 为 c<d,所以最终结果为 A
7. Assume that a and b are variables of type int. The expression
!(a < b) && !(a > b)
is equivalent to which of the following?
(A) true
(B) false
(C) a == b
(D) a != b
(E) !(a < b) && (a > b)
答案:C 思路 1: 优先级 !>& 所以!(a < b)和!(a > b) 先算,之后再&,!(a < b)=> a>=b ,
!(a > b) => a <= b,整个式子相当于(a>=b)&&(a<=b)这个式子只有在 a==b 时为 true a!=b 时为 false 所以它
等同于 a==b
8. Assume that the boolean variables a and b have been declared and initialized. Consider the following expression.
(a && (b || !a)) == a && b
Which of the following best describes the conditions under which the expression will evaluate to true?
(A) Only when a is true
(B) Only when b is true
(C) Only when both a and b are true
(D) The expression will never evaluate to true.
(E) The expression will always evaluate to true.
答案:B boolean 式题目天花板级别。首先()优先界最高,左边()部分先看成整体,==优先级别高于&& 所
以先算()式==a 的结果再 &&b
(a && (b || !a))是 A&&(B||C)式子转换为(A&&B)||(A&&C) => (a&&b)||(a&&!a)=> a&&!a 即为 false,所以式子变
为 (a&&b)||false,A||false==>A,所以式子相当于(a&&b),
左边(a && (b || !a))式子最终转换结果为 a&&b 接下来是(a&&b)==a && b, 相当于((a&&b)==a )&& b
题目问这个式子什么时候为 true,所以首先 b 要为 true,然后检验 b 为 true 时(a&&b)==a 是否也为 true,b 为 true 时此式
子变为 a == a 为 true,所以只有 b 为 true 时整个式子为 true。
假设此题 b 为 true 时((a&&b)==a )是其他推导结果为 false 的式子,则整个式子永远为 false
The following incomplete method is intended to return the total cost of an order based on the value of the
parameter numBoxes.
Which of the following code segments can be used to replace /* missing code */ so that method getCost will work
as intended?
(A) I only
(B) II only
(C) III only
(D) I and II
(E) II and III
III if 和 else 一定是互斥的所以 if (numBoxs > 0)的 elseif 条件默认先满足 numBox<=0, numBoxes 就不可能再>=5
I if(num>10)的一定满足 if(num>=5)一定满足(num>0)所以 num>10 的最终结果会被 num>0 覆盖,并不是 num>10
真正结果 同理 num>=5 也是被 num>0 覆盖
但是如果 I 改成
if(numBox > 0)
{
totalCost = numBoxes * 5.0;
}
if(numBox > 5)
{
totalCost = numBoxes * 3.0;
}
if(numBox > 10)
{
totalCost = numBoxes * 1.5;
}
就还是对的,因为 numBox>0 的但是小于 5 的无法走 if(numBox>5)的条件,还是相当于 numBoxs >0
& numBoxs < 5, 然后 numBox >5 的虽然走了 num>0 的条件,但是还是会走 num>5,最终是 num>5 的结果,所以
没问题。
b. if if 并列结构中 statement 均包含 return 语句
if(A)
{
statement A;
return XXX;
}
if(B)
{
statement B;
return XXX;
}
if(C)
{
statement C;
return XXX;
}
这种结构每个 if 条件均包含 return 语句,return 语句立刻结束函数,所以如果满足条件 A,直接 return 不会再走
statementB,如果可以走 statementB,说明条件满足 !A&&B,同理如果满足!A&&B,不会走 C 直接 return 结束函
数,如果满足!A&&!B&&C 则可以走到 statementC
所以此 if 结构等同于
if(A)
{
statement A;
return XXX;
}
else if(B)
{
statement B;
return XXX;
}
else if(C)
{
statement C;
return XXX;
}
例:9. Consider the following incomplete method.
public int someProcess (int n)
{
/* body of someProcess */
}
The following table shows several examples of input values and the results that should be produced by calling
someProcess .
3 30
6 60
7 7
8 80
9 90
11 11
12 120
14 14
16 160
which of the following code segments could be used to replace /* body of someProcess */ so that the method
will produce the results shown in the table?
I. if ( (n % 3 == 0) && (n % 4 == 0))
return n * 10;
else
return n;
II. if ( (n % 3 == 0) | | (n % 4 == 0))
return n * 10;
return n;
III. if (n % 3 == 0)
if (n % 4 == 0)
return n * 10;
return n;
(A) I only
(B) II only
(C) III only
(D) I and III
(E) II and III
答案:B n % 3 == 0 的数,即为 3 的倍数,此题中 3,6,8,9,12,16 等被 3 或 4 整除的数,结果是参数*10, 其他情
况结果为参数值本身,所以选 II,
III if 嵌套 if 相当于 (n % 3 == 0) && (n % 4 == 0)又因为 statement 中包含 return 所以 I 和 III 其实相同
10. Consider the following instance variables and method that appear in a class representing student information.
• The student has a test average that is greater than or equal to 90.
• The student has a test average that is greater than or equal to 75 and has at least 4 completed
assignments. Consider the following proposed implementations of the isPassing method.
(A) I only
(B) II only
(C) I and III only
(D) II and III only
(E) I, II, and III
答案 E 第一个如果 testAverage>=90 立刻 return 结束函数可以,如果不满足 testAverage>=90 走第二个 if 如果
满足条件 testAverage >= 75 && assignmentsCompleted >= 4 就返回 true,都不满足就返回 false
第二个并列 if 也包含了两种情况都可以返回 true,没问题
第三题是两个条件至少满足其一就 true 所以是||没问题
11. Consider the following code segment, which is intended to simulate a random process. The code is intended to set the
value of the variable event to exactly one of the values 1, 2, or 3, depending on the probability of an event occurring.
The value of event should be set to 1 if the probability is 70 percent or less. The value of event should be set to 2 if
the probability is greater than 70 percent but no more than 80 percent. The value of event should be set to 3 if the
probability is greater than 80 percent. The variable randomNumber is used to simulate the probability of the event
occurring.
int event = 0;
if (randomNumber <= 0.70)
{
event = 1;
}
if (randomNumber <= 0.80)
{
event = 2;
}
else
{
event = 3;
}
The code does not work as intended. Assume that the variable randomNumber has been properly declared and
initialized. Which of the following initializations for randomNumber will demonstrate that the code segment
will not work as intended?
(A) randomNumber = 0.70;
(B) randomNumber = 0.80;
(C) randomNumber = 0.85;
(D) randomNumber = 0.90;
(E) randomNumber = 1.00;
答案 A 这题因为小于 7.0 的也一定满足小于 8.0,所以满足 7.0 的结果被 8.0 覆盖,所以所有<7.0 分数的都
不对。选 A
四、for_loop,while_loop
1. 循环一维数组或 arrayList 时注意边界,注意递增变化
若出现 arr[i-1], int i =1 开始,若出现 arr[i+1], i < length-1
循环的递增变化有时非 i++,而是 i = i+2 等
例:14. Consider the following method, which is intended to return the number of local maximum values in an array. Local
maximum values are array elements that are greater than both adjacent array elements. The first and last elements of
an array have only a single adjacent element, so neither the first nor the last array element is counted by this
method.For example, an array containing the values {3, 9, 7, 4, 10, 12, 3, 8} has two local maximum values:
9 and 12.
public static int countPeaks (int [] data){
int numPeaks = 0;
for ( /* missing loop header */ )
{
if( data[i] > data[i-1] && data[i] > data[i+1])
{
numPeaks++;
}
}
return numPeaks;
}
Which of the following can replace /* missing loop header */ so the method countPeaks works as intended?
(A) int p = data.length - 1; p > 0; p--
(B) int p = 0; p < data.length; p++
(C) int p = 0; p < data.length - 1; p++
(D) int p = 1; p < data.length; p++
(E) int p = 1; p < data.length - 1; p++
答案:E
2 双重 for 循环
双重 for 循环注意外层循环执行一次,内层循环执行一轮
打印内容等,先按照内层循环开始执行,然后是外层代码,
例:a. 二维数组 row-major 打印元素时列号递增,打印一行后,行号递增,所以列递增写在内层循环,行递
增写在外层循环。且先按照内存循环的递增情况执行代码!然后再按照外层循环执行
b.双重 for 循环打印三角形等,外层循环决定行数,内层循环决定每一行打印的内容
考题类型: 1 循环执行次数,2 循环执行结果,打印图形等 3 外层/内层循环代码改变对循环次数的影响。
例:15. Consider the following code segment.
int outerMax = 10;
int innerMax = 5;
for (int outer = 0; outer < outerMax; outer++){
for (int inner = 0; inner <= innerMax; inner++)
{
System.out.println (outer + inner);
}
}
How many values will be printed when the code segment is executed?
(A) 45
(B) 50
(C) 55
(D) 60
(E) 66
答案:D 执行次数外层 10 次内层 6 次,所以总次数 60
16. Consider the following method.
public int sol (int lim)
{
int s = 0;
for (int outer = 1; outer <= lim; outer++)
{
for (int inner = outer; inner <= lim; inner++)
{
s++;
}
}
return s;
}
What value is returned as a result of the call sol(10)?
(A) 20
(B) 45
(C) 55
(D) 100
(E) 385
答案:C 外层 loop1,inner 执行 1-10,10 次,loop2,inner 执行 2-10,9 次。。。Loop10,inner 执行 1 次所以
最终次数为 1+2+3...+10 答案是 C
17. The question refer to the following code segment.
int k = a random number such that 1 ≤ k ≤ n ;
for(int p = 2; p <= k; p++)
for(int r = 1; r < k; r++)
System.out.println("Hello");
What is the maximum number of times that Hello will be printed?
(A) 2
(B) n - 1
(C) n - 2
(D) (n - 1)2
(E) n2
答案:D,最大次数就是 k = n 的时候,所以外层是 2-n,一共 n-1 此,内层 1 到 n-1 也是 n-1 次,所以答案是 D
18. The question refer to the following code segment.
int k = a random number such that 1 ≤ k ≤ n ;
for(int p = 2; p <= k; p++)
for(int r = 1; r < k; r++)
System.out.println("Hello");
What is the minimum number of times that Hello will be printed?
(A) 0
(B) 1
(C) 2
(D) n - 1
(E) n - 2
答案:答案 A 如果 k=1 外层不会执行
19. Consider the following output.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
Which of the following code segments will produce the output shown above?
(A) for(int j = 1;j <= 6; j++)
{
for (int k = 1; k < j; k++)
System.out.print(" " + k);
System.out.println();
}
(B) for(int j = 1;j <=6;j++)
{
for (int k = 1; k <= j; k++)
System.out.print(" " + j);
System.out.println();
}
(C) for( int j = 1; j <=6;j++)
{
for (int k = 1; k <= j; k++)
System.out.print(" " + k);
System.out.println();
}
(D) for( int j = 1; j< 6; j++)
{
for (int k = 1; k <= j; k++)
System.out.print(" " + k);
System.out.println();
}
(E) for( int j = 1; j< 6;j++)
{
for (int k = 1; k < j; k++)
System.out.print(" " + k);
System.out.println();
}
答案:C 这种题型,外层循环决定最终结果的行数,内层循环决定每行打印的内容。最终结果是 6 行,所以外
层循环 6 次,内层循环次数从 1 到 6 递增,所以内层循环参数的变化一定和外层循环有关,这时检查代码的内层
循环的最小值和最大值是否满足第一次循环是 1 最后一次是 6 即可。此时 BC 都满足条件,还要具体看内层循环
打印的是外层参数还是内层参数,因为打印结果是123递增所以打印的是内层变化值。所以此题选 C
只要出现三角形,内层循环变化的参数一定和外层相关。
C.ArrayList:
For ( type(有时是 class 名) 变量名:list 名)
{
变量名.method()
}
ArrayList:
ArrayList < ClassName> list;
for( ClassName obj : list)
{
obj.ClassMethod(); // obj is list.get(i)
}
例题:
20. Consider the following incomplete method, which is intended to return the sum of all the elements in its
array parameter.
/**Precondition:data.length 0*/
public static int total(int[] data)
{
int result = 0;
/* missing code */
return result;
}
The following code segments have been proposed to replace /* missing code */.
Which of the replacements for /* missing code */ could be used so that total will work as intended?
(A) I only
(B) II only
(C) III only
(D) I and II
(E) II and III
答案:E,enhanced for 最常见题型,I 不对是因为加的是 index
The following code segment is intended to store in maxPages the greatest number of pages found in any Book
object in the array bookArr.
Which of the following can replace /* missing code */ so the code segment works as intended?
(A) 0 0 0 0 0 0
(B) 0 0 0 0 0 6
(C) 123456
(D) 123450
(E) changeIt will throw an exception.
答案:C,
这道题考察的是 method 并不能真正改变实参变量的值。就是实际参数进入 changeIt 进行了运算,但是在 changeIt 结
束后,这些实际参数的变量 value name 的值并没有真的发生改变。
但是正常来说,数组作为实际参数的情况下在调用结束后值也是会变的,但是这道题因为用了 new 所以没有改变。
在start里面调用changeIt(nums, value, name)的时候他们会把他们的值分别赋值 nums{1,2,3,4,5} =>
arr,value 6=> val, 然后在 changeIt 里面本来 arr 的初始值是{1,2,3,4,5}
但是因为它第一句就是 arr = new int[5]; 所以 arr 又新建了新的数组,在内存里重新申请了 5 个int 的空间,指向了新
的数组,arr 的地址和 nums 的地址不再相同,不再指向相同的数组的起始位置,所以 arr 后面的任何数组的操作对 nums
中的值都没有产生影响
4.一维数组算法,遍历时找最大值最小值,reverse 整个数组,判断连续序列长度等,具体算法见 FRQ
葵花宝典,下面列举几道经典例题:
例:26. 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?
(A) Integer.MAX_VALUE
(B) Integer.MIN_VALUE
(C) 0
(D) word.length()
(E) wordArray.length
答案:A 寻找最小值,所以要保证数组里的值一定能小于标准值,才能把数组里的值赋值给存储最小值的变量
27.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)
(A) {
maxIndex = k;
}
}
return words[maxIndex];
int maxIndex = 0;
for (int k = 1; k <= words.length; k++)
{
if (words[k].compareTo(words[maxIndex]) > 0)
(B) {
maxIndex = k;
}
}
return words[maxIndex];
int maxIndex = 0;
for (int k = 1; k < words.length; k++)
{
if (words[k].compareTo(words[maxIndex]) > 0)
(C) {
maxIndex = k;
}
}
return maxIndex;
答案:E 寻找最大值的常见算法
Which of the following describes what the method arrayMethod() does to the array nums?
(A) The array nums is unchanged.
(B) The first value in nums is copied to every location in the array.
(C) The last value in nums is copied to every location in the array.
(D) The method generates an ArrayIndexOutOfBoundsException.
(E) The contents of the array nums are reversed.
What are the contents of resultOne when the code segment has been executed?
(A) {"day", "first", "of", "spring"}
(B) {"of", "day", "first", "spring"}
(C) {"of", "day", "of", "spring"}
(D) {"of", "of", "of", "spring"}
(E) {"spring", "first", "day", "of"}
34. Consider the following code segment.
How many times is the line labeled // Line 12 in the strArrMethod executed as a result of executing the
code segment?
(A) 4 times
(B) 5 times
(C) 6 times
(D) 15 times
(E) 30 times
答案 33 D 34 A
String[] result = new String[arr.length];
for (int j = 0; j < arr.length; j++)
{
String sm = arr[j]; //每次把 j 位置的字符串赋值给 sm
for (int k = j + 1; k < arr.length; k++) //遍历 j 位置后面的所有字符串
{
if(arr[k].length() < sm.length()) //如果 j 后面的字符串长度小于 j 位置字符串的长度
{
sm = arr[k]; // Line 12 //j 后面字符串长度小于 sm 的长度就更新 sm,所以每发现一个
长度更短的字符串就会调用这行代码
}
} //for 循环结束
result[j] = sm; //循环结束后,将 j 位置后面最短的字符串替换 j 位置的字符串
}
return result;
第二问 A
String[] testTwo = {"last", "day", "of", "the", "school", "year"};
String[] resultTwo = strArrMethod(testTwo);
对于"last"来说,一开始 sm = "last","day"比"last"短,所以走 line12,sm 变成"day",然后"of"比"day"短,走 line12,
sm 又变成"of",之后的"the", "school", "year"都比"of"长,所以不更新不走 line12,对于 last 来说 line12 走两次
然后 sm 变成"day","of"比"day"短,所以走 line12,sm 又变成"of",之后的"the", "school", "year"都比"of"长,所以
不更新不走 line12,对于"day",line12 走 1 次
然后 sm 变成"of",之后的"the", "school", "year"都比"of"长,所以不更新不走 line12,对于"of",line12 走 0 次
然后 sm 变成"the",之后的 "school", "year"都比"the"长,所以不更新不走 line12,对于"the",line12 走 0 次
然后 sm 变成"school",之后的"year"比"school"短,走 line12 更新 1 次,所以对于"school",line12 走 1 次
最后 line12 一共走 4 次
九、二维数组
1. 二维数组主要一定要清楚循环的是行还是列
for(int i = 0; i < arr2D.length; i++) 循环的是行!!!
for(int j = 0; j < arr2D[0].length;j++) 循环的是列!!!
2.row-major 和 col-major
row-major 一行一行循环,所以外层循环是行,内层循环是列
for(int i = 0; i < arr2D.length; i++) {
for(int j = 0; j < arr2D[0].length;j++)
Col-major 一列一列循环,所以外层循环是列,内层循环是行。
for(int i = 0; i < arr2D[0].length; i++) {
for(int j = 0; j < arr2D.length;j++)
3. 对角线位置的值(行==列)
对角线上面的值 (行>列)
对角线下面的值 (行<列)
二维数组选择题主要是需要细心就没问题!
例:
35. Consider the following code segment.
37.Consider the following method, count, which is intended to traverse all the elements in the two-dimensional
(2D) String array things and return the total number of elements that contain at least one "a".
The method does not always work as intended. For which of the following two-dimensional array input values does
count NOT work as intended?
(A) {{"lemon"}, {"lime"}}
(B) {{"tall", "short"}, {"up", "down"}}
(C) {{"rabbit", "bird"}, {"cat", "dog"}, {"gecko", "turtle"}}
(D) {{"scarf", "gloves", "hat"}, {"shoes", "shirt", "pants"}}
(E) {{"math", "english", "physics"}, {"golf", "baseball", "soccer"}}
答案是 D,注意上面的双重 for 循环时 c < things [r].length - 1 循环的是列,且没有循环最后一列,所以当
最后一列出现字母 a 的时候代码是不符合题意的。
十、递归(必会,大概考三题)
递归最重要 1 要有终止条件,终止条件一定是在递归语句前面,否则终止不了
38. Consider the following instance variable and methods. You may assume that data has been initialized with length > 0.
The methods are intended to return the index of an array element equal to target, or - 1 ifno such element exists.
For which of the following test cases will the call seqSearchRec(5) always result in an error?
(A)
(B)
(C)
(D)
(E)
}
Which of the following corrects the method maxHelper so that it works as intended?
答案:C
start 到数组最后一个元素时终止,所以 start 值应该在调用过程中递增
1.Consider the following method.
public static int mystery(int n)
{
if (n <= 0)
{
return 0;
}
else
{
return n + mystery(n – 2);
}
}
What value is returned as a result of the call mystery(9) ?
(A) 0
(B) 9
(C) 16
(D) 24
(E) 25
答案:E
答案 E:
// precondition: x >= 0
public void mystery(int x)
{
if ((x / 10) != 0)
{
mystery(x / 10);
}
System.out.print(x % 10);
}
(A) 1234
(B) 4321
(C) 12344321
(D) 43211234
(E) Many digits are printed due to infinite recursion.
答案 D
4.Consider the following recursive method.
System.out.println(recur(32));
(A )20
(B )102
(C )210
(D )1020
(E )2101
(C) 7531
(D) Many numbers will be printed because of infinite recursion.
(E) No numbers will be printed because of infinite recursion
答案:E
6.Consider the following two methods, which are intended to return the same values when they are called with the
same positive integer parameter n.
Which, if any, of the following changes to mystery2 is required so that the two methods work as intended?
(A) The variable total should be initialized to 1.
(B) The variable x should be initialized to 0.
(C) The condition in the while loop header should be x < n - 1.
(D) The condition in the while loop header should be x <= n.
(E) No change is required; the methods, as currently written, return the same values when they are called with
安安
the same positive integer parameter n.
答案:(A)m(1)return 1 相当于 m2 的 total 初始值所以 total 应该为 1
7.Consider the following method.
public String goAgain (String str, int index)
{
if (index >= str.length ())
return str;
return str + goAgain (str.substring (index), index + 1);
}
What is printed as a result of executing the following statement?
System.out.println (goAgain ("today",1);
(A) today
(B) todayto
(C) todayoday
(D) todayodayay
(E) Todayodaydayayy
答案:D
8. Consider the following method.
public static void strChange (String str)
{
if (str.length () > 0)
{
strChange (str.substring (1));
System.out.print (str.substring (0, 1));
}
}
Which of the following best describes the behavior of the method?
(A) It prints the first character of str.
(B) It prints the last character of str.
(C) It prints the characters of str in the order they appear.
(D) It prints the characters of str in reverseorder.
(E) It prints nothing due to infinite recursion.
答案 D
return max;
else
return nums [numVals - 1];
}
Which of the following best describe the condition under which MaxHelper doesn’t work?
(A) When numVals is 1
(B) When numVals is even
(C) When the elements of nums are in nonincreasing order
(D) When the elements of nums are in nondecreasing order
(E) Method maxHelper never works as intended.
答案: E 因为没有终止条件,不知道 numVals-1 会到什么时候终止,所以永远都不对。
Unit_8 ArrayList Summary
ArrayList is a dynamic array which can resize itself any time, and very flexible of insertion
and delete operations.
A. ArrayList elements:
ArrayList can only store object type elements , so int or double or boolean cannot be
elements of arrayList
Special Case:
- ArrayList repensents any type of elements
ArrayList listName = new ArrayList<>();
- ArrayList repensents more than one type of elements
ArrayList listName = new ArrayList<>();
1. Consider the following statement, which is intended to create an ArrayList named values that can be
used to store Integer elements.
/* missing code */ = new ArrayList<>();
Which of the following can be used to replace /* missing code */ so that the statement compiles without
error?
I. ArrayList values
II. ArrayList<int> values
III. ArrayList<Integer> values
(A) I only
(B) II only
(C) III only
(D) I and III only
(E) II and III only
2. Consider the following statement, which is intended to create an ArrayList named years that can be
used to store elements both of type Integer and of type String.
/* missing code */ = new ArrayList();
Which of the following can be used to replace /* missing code */ so that the statement compiles without
error?
(A) ArrayList years
(B) ArrayList years()
(C) ArrayList years[]
(D) ArrayList<Integer> years
(E) ArrayList<String> years
B. ArrayList operations
- Create ArrayList
ArrayList<Type> list = new ArrayList<Type>(); Note :List is empty without any elements
ArrayList<String> strList = new ArrayList<String>();
ArrayList<Integer> intList = new ArrayList<Integer>();
ArrayList<Double> douList = new ArrayList<Double>();
In AP exam, normally the type is another self-defined class
Example:
public class Cat{
private int size;
private String color;
public Cat() //constructor method1
{
size = 1;
color = “white”;
}
public Cat(int size, String color) //constructor method2
{
this.size = size;
this.color = color;
}
public class PetStore{
private ArrayList<Cat> list;
public PetStore() /PetStor constructor,initialize instance variable ArrayList<Cat> list
{
ArrayList<Cat> list = new ArrayList<Cat>();//list contains Cat type objects
for(int i = 0; i < list.size(); i++)
list.add(new Cat());
//Cat() method is Cat class constructor, new Cat() create a new Cat object
}
public PetStore(int[] sizes, String[] colors){//也可以调用 catClass 另一个 constructor
ArrayList<Cat> list = new ArrayList<Cat>(); //list contains Cat type objects
for(int i = 0; i < list.size(); i++)
list.add(new Cat(size[i], colors[i]));
//Cat(int size, String color) is another Cat class constructor, new Cat(size[i],colors[i]) create
a new Cat object
}
}
- add(Elements)
arrayListName.add(object); /add at the end of the arrayList
add( index, Elements)
arrayListName.add(i,object); /add at pos i, previous elements( i to end) automatically
shift right
- E set(index, Elements)
arrayListName.set(index, newObject);
Type E = arrayListName.set(index, newObject);
Note: - set operation replace object at pos index with newObject
- set operation has return type value object, the object be replaced, but use or not
use the returned object is both ok.
3. Consider the following code segment.
ArrayList<String> numbers = new ArrayList<String>();
numbers.add("one");
numbers.add("two");
numbers.add(0, "three");
numbers.set(2, "four");
numbers.add("five");
numbers.remove(1);
Which of the following represents the contents of numbers after the code segment has been
executed?
(A) ["one", "four", "five"]
(B) ["three", "two", "five"]
(C) ["three", "four", "two"]
(D) ["three", "four", "five"]
(E) ["one", "two", "three", "four", "five"] (D)
-E get(index)
arrayListName.get(index,); //Obtain an object at pos index in arrayList
Code2:
int i = 0;
while( i <list.size()){
if(...)
list.remove(i); //when remove, do not increase i;
else
i++; //increase i only when no remove operation happens
}
2. Remove has return type, return the removed object, which is always
used in FRQ
For Example
Create a new list to store removed objects:
ArrayList<Type> rList = new ArrayList<Type>();
for(int i = oldList.size(); i > 0; i--){ //remove FRQ always traverse backwards!!!!
if(...){
rList.add(oldList.remove(i)); //directly add removed item in new list
}
}
Remove MCQ questions, skip is still the most important point
3. The removeElement method is intended to remove all instances of target from the ArrayList
object data passed as a parameter. The method does not work as intended for all inputs.
public void removeElement(ArrayList<Integer> data, int target)
{
for (int j = 0; j < data.size(); j++)
{
if (data.get(j).equals(target))
{
data.remove(j);
}
}
}
Assume that the ArrayList object scores and the int variable low_score have been properly
declared and initialized. In which of the following cases will the method call removeElement(scores,
low_score) fail to produce the intended result?
(A) When scores is [0, 2, 0, 2, 0, 6] and low_score is 0
(B) When scores is [2, 4, 0, 5, 7, 0] and low_score is 0
(C) When scores is [3, 4, 5, 7, 7, 2] and low_score is 1
(D) When scores is [8, 8, 4, 3, 3, 6] and low_score is 3
(E) When scores is [9, 9, 5, 9, 7, 7] and low_score is 5
解析:8 8 4 3 3 6 假如找 8 代表 i 循环到的元素,所以 8 被跳过去了
Index 01234
此题条件是 if (data.get(j).equals(target))
不管是什么条件,比如 if (data.get(j)%2==0) 原题 intend remove all even number,如果有两个偶数连着,
第二个偶数还是会被 skip
所以只要有两个都符合条件的数连着,第一个被 remove 了之后,第二个就会被 skip!!!!
解析: 2 7 5 5 5 5 6 6 3 3 3 (C)
代表 i 循环到的元素, 只有发生删除的时候, 才会跳!
3. Consider the following method, which is intended to return a list containing the elements of the parameter
myList with all even elements removed.
public static ArrayList<Integer> removeEvens(ArrayList<Integer> myList)
{
for (int i = 0; i < myList.size(); i++)
{
if (myList.get(i) % 2 == 0)
{
myList.remove(i);
}
}
return myList;
}
Which of the following best explains why the code segment does not work as intended?
(A) The code segment causes an IndexOutOfBoundsException for all lists because of an incorrect
Boolean expression in the for loop
. (B) The code segment causes an IndexOutOfBoundsException for lists with at least one even element
because the indexes of all subsequent elements change by one when a list element is removed
(C) The code segment returns a list with fewer elements than intended because it fails to consider the last
element of myList.
(D) The code segment removes the wrong elements of myList because the condition in the if statement to
test whether an element is even is incorrect.
(E) The code segment skips some elements of myList because the indexes of all subsequent elements
change by one when a list element is removed
Answer (E)
Assume that inputList is an ArrayList of Integer objects that contains the following values.
How many calls to mystery (including the initial call) are made as a result of the call mystery(arr, 0, arr.length - 1,
14) if arr is the following array?
0 1 2 3 4 5 6 7
arr 11 13 25 26 29 30 31 32
(A) 1
(B) 2
(C) 4
(D) 7
(E) 8
选择排序思想:第一次循环找出最小值放在第一个位置,第二次循环找出第二小的放在第二个位置,第三次循环找
出第三小放在第三个位置,所以每次循环后,前面的序列逐渐变为递增的有序序列直到整个数组遍历结束。
所以每次查找时,序列分为三个部分:
2,3,4,5,4,6,7 2,3 是已经排好序的部分,4 是待交换的值,5,8,6,7 待排序序列
选择排序代码:
public static void selectionSort(int arr[]){
for (int i = 0; i < arr.length - 1; i++){
int min_idx = i; //i 即为每次要被交换的位置,i 赋值给 min_idx 保证被交换的值也能在 寻找
最小值过程中包含在内
选择排序快速判断排序结果的方法
1 根据代码是否有交换值代码判断是否是选择排序
2 判断循环中找的是最大值还是最小值
3 看最初始最外层循环起始位置,如果是 0 就是把最大值或者最小值放在第一个位置,如果是 length-1 就是把最小值
放在最后位置,来判断选择排序最终生成的是递增还是递减序列
Page 18 of 19 AP Computer Science A
选择排序考察题型:选择题,
考查知识点:
1 寻找最小值的语句执行次数,交换代码执行次数等,所以必须非常清楚选择排序的过程
2 寻找最小值或者最大值时一定要记得包含将被交换的那个值,判断哪个代码正确
3 给定待排序数组或者序列,判断排序结果,递增还是递减
例题:
Consider the following correct implementation of the selection sort algorithm.
public static void selectionSort (int [] elements){
for (int j = 0; j < elements.length - 1; j++)
{
int minIndex = j;
for (int k = j + 1; k < elements.length; k++)
{
if (elements [k] < elements [minIndex])
{
minIndex = k;
}
}
if (j != minIndex)
{
int temp = elements [j];
elements [j] = elements[minIndex];
elements [minIndex] = temp; // Line 19
}
}
}
The following declaration and method call appear in a method in the same class as selectionSort.
int[] arr = {9, 8, 7, 6, 5};
selectionSort (arr);
How many times is the statement elements [minIndex] = temp; in line 19 of the method executed
as a result of the call to selectionSort ?
(A) 1
(B) 2
(C) 3
(D) 4
(E) 5
9 8 7 6 5 考查交换次数 第一次循环 9 和 5 交换 序列变为 5 8 7 6 9
第二次循环 从 8 开始找最小值 6,6 和 8 交换,序列变为 5 6 7 8 9 序列已经有序结束,所以交换代码执行了 2 次
7. Consider the following correct implementation of the selection sort algorithm.
public static void selectionSort(int arr[]){
for (int i = 0; i < arr.length - 1; i++){
int min_idx = i;
for (int j = i+1; j < arr.length; j++){
if (arr[j] < arr[min_idx])
min_idx = j;
}
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
Page 19 of 19 AP Computer Science A
The following declaration and method call appear in the same class as selectionSort.
How many times is the statement minIndex = k; in line 11 of the method executed as a result of the call
to selectionSort ?
(A) 0
(B) 1
(C) 2
(D) 3
(E) 4
解析:minIndex = k;语句是寻找最小值过程中,每次找到更小的值就赋值位置 k 给 minIndex
5 10 2 1 12 第一次循环时 被交换值为 5, 2 < 5 minIndex 变为 2 的位置,1 小于 2minIndex 变为 1 的位置执行了
2 次,序列变为 1 10 2 5 12
第二次循环时,被交换值为 10, 2 < 10, minIndex 变为 2 的位置,后面没有比 2 小的,不再交换,执行 1 次,
序列变为 1 2 10 5 12
第三次循环时,被交换值为 10, 5<10 minIndex 变为 5 的位置,后面没有比 5 更小的,执行 1 次,序列变为 1
2 5 10 12 已有序,结束。所以执行了 4 次
C. InsertionSort 插入排序
插入排序算法概述,数组 8,5,4,3,2,6,7 为例
插入排序从数组第二个数开始,第二个数即为要插入的数,如果比前一个数小,则前一个数后移,第二数放在第
一个位置,如果大于等于前一个数,不变
接下来第三个数,第三个数即为要插入的数,如果比前面数大,前面数后移,直至前面的数小于当前要
被插入的数为止,被插入的数找到了自己的位置,插入该位置
接下来递增要插入的数,直到最后一个。
数组 8,5,4,3,6,7 为例
Loop1 5 为要插入的数 5 < 8 ,8 后移,5 插入第一个位置,数组变为 5,8,4,3,2,6,7
Loop2 4 为要插入的数,4 < 8, 8 后移一位,4 < 5,5 后移一位,4 插入第一位,序列变为 4,5,8,3,2,6,7
Loop3 3 为要插入的数,3 < 8, 8 后移一位,3 < 5,5 后移一位,3<4, 4 后移一位, 3 插入第一位,序列变为
3,4,5,8,6,7
Loop4 6 为要插入的数,6 < 8, 8 后移一位,6 > 5,6 插入,序列变为 3,4,5,6,8,7
Loop4 7 为要插入的数,7 < 8, 8 后移一位,7 > 6,7 插入,序列变为 3,4,5,6,7,8
D. MergeSort 排序
排序算法,将序列不断分为两半分两半直到每半只有一个元素为止
然后 merge 一个和一个排好序 merge 成两个 两个和两个排好序 merge 成四个。。。一直到最后
以 8,5,4,3,2,6,7,9 为例
1 拆成 8 5 4 3 和 2 6 7 9
2 再拆成 8 5 和 4 3 和 2 6 和 7 9
3 再拆成 8 和 5 4 和 3 2 和 6 7 和 9
4 merge: 8 和 5 merge 成 5 8 4 和 3 merge 成 3 4 2 和 6merge 成 2 6 7 和 9merge 成 7 9
5 merge: 5 8 和 3 4 merge 成 3 4 5 8, 2 6 和 7 9 merge 成 2 6 7 9
6 merge: 3 4 5 8 和 2 6 7 9 merge 成 2 3 4 5 6 7 8 9
extends左边为子类,右边为父类
子类继承表现两种行为:继承和Extend
继承(inheritance):
b. 子类没有继承父类的: constructor函数
Extend:
a. 子类可以声明自己新的instance variable(父类中没有的)
b. 子类可以生明自己新的public method(父类中没有的)
c. 子类可以改写(override)父类中继承的public method,即父类
子类函数名相同,参数列表相同,函数内容不同(表现不同的行为)
d. 子类必须重写自己的constructor函数
- 并且通常显式(explicitly)通过super关键字调用父类的构造函
数,无参构造函数或有参构造函数,有时可能会出现父类无无参构造函
数或者子类没有显式通过super调用父类构造函数的情况。见下方详解。
二、Inheritance常考题型:
A. 父类new 子类,子类new子类等
obj 只能调用父类中的所有函数,且表现父类的行为
obj 只能调用父类中的所有函数,不能调用子类中自己的新函数,
Test Booklet
且如果obj调用了被子类改写的父类函数,则表现子类的行为。
如果obj调用的父类函数没有被子类改写,则仍表现父类的行为,即相当
于子类继承来的相同的行为。
因为java编译时,代码从左往右读,会认为obj是个父类的obj,所以obj
只 能 调 用 父 类 中 的 函 数 。 但 是 运 行 时 发 现 obj 实 际 是 调 用 的 子 类 的
constructor函数构造的对象,所以运行时会表现子类的行为。
obj 可以调用父类和子类中的所有函数,因为父类中的函数全部都被子
类继承,可调用;子类自己新创建的函数,子类也可以调用。且全部表现子
类的行为。
错误!!!越级!!!
例题在知识点总结后。
B. 子类通过super关键字表现父类的行为
子类函数中表现一部分父类的行为,即在子类的非构造函数中通过super.(点)
父类函数名调用父类的函数,然后继续执行子类函数的语句,接着表现子类的行
为。
public class Artifact
{
private String title;
private int year;
public Artifact(String t, int y)
{
title = t;
year = y;
}
public void printInfo()
{
System.out.print(title + " (" + year + ")");
}
}
Test Booklet
子类调用父类的构造函数分为以下四种情况
1)最通常的情况父类中既包含无参构造函数,又包含有参构造函数
public class superClassName{
type instanceVar1;
type instanceVar2;
public superClassName()
{
instanceVar1 = default value;
instanceVar2 = default value;
}
public superClassName(type var1, type var2)
{
instanceVar1 = var1;
instanceVar2 = var2;
}
}
子类显式(explictily)调用父类的无参或者有参构造函数,初始化父类的
instance variables
Test Booklet
//子类的无参构造函数可以显式调用父类的无参或有参构造函数
public subClassName()
{
super();
newInstanceVar3 = default value;
}
或者:
public subClassName()
{
super( type var1,type var2);
newInstanceVar3 = default value;
}
//子类的有参构造函数也可以显式调用父类的无参或有参构造函数
public subClassName(type var3)
{
super();
newInstanceVar3 = var3;
}
或者:
public subClassName(type var3)
{
super( type var1,type var2);
newInstanceVar3 = var3;
}
}
2)父类中既包含无参构造函数,又包含有参构造函数,但是子类没有显
式的调用父类的构造函数,即子类构造函数中没有出现super关键字,这时子类
会隐式的(implicitly)调用父类的无参构造函数,注意是调用无参的!隐式指的
是没有明确的写出super但是程序的运行过程中仍然会去调用会根据super的无
Test Booklet
参构造函数的代码完成superClass的instance variable的初始化赋值。
- 父类仍既包含无参构造函数和有参构造函数
public class superClassName{
type instanceVar1;
type instanceVar2;
public superClassName() //Line4
{
instanceVar1 = default value; //Line5
instanceVar2 = default value; // Line6
}
public superClassName(type var1, type var2){
instanceVar1 = var1;
instanceVar2 = var2;
}
}
- 子类的构造函数没有明确写出super关键字
public class subClassName extends superClassName{
type newInstanceVar3;
public subClassName()
{
newInstanceVar3 = default value;
}
public subClassName(type var3)
{
newInstanceVar3 = var3;
}
}
子类的无参构造函数无super关键字,但子类仍会implicitly调用父类的无
参构造函数
即上面的line3,line4,line5将父类的instanceVar1和instanceVar2初
始化为默认值
public subClassName(){
newInstanceVar3 = default value;
}
Test Booklet
子类的有参构造函数无super关键字,但子类仍会implicitly调用父类的
无参构造函数
即上面的line3,line4,line5将父类的instanceVar1和instanceVar2
初始化为默认值
public subClassName(type var3)
{
newInstanceVar3 = var3;
}
3)父类中并没有明确写出无参的构造函数,但是明确写了有参构造函数,
且子类显式的调用父类的无参构造函数。注意,此时java不会给父类提供默认的
无参构造函数。 报错!!!此知识点CB未出过题目
代码示例如下:
注意:父类中并没有明确写出无参构造函数,但是有明确写了有参构造
函数
public class Bike {
private int numOfWheels = 2;
4)父类中并没有明确写出无参的构造函数,也没有有参构造函数,即父类没
有任何构造函数的情况下!Java会给父类提供默认的无参构造函数!
构造函数 × (因为父类并没有有参的构造函数)
所有父类的instance variable都初始化为默认值
public class Bike {
private int numOfWheels = 2;
/* no constructor method*/
public int getNumOfWheels(){
return numOfWheels;
}
}
父类没有任何构造函数,java才会提供一个默认的无参构造函数。
public class EBike extends Bike{
private int numOfWatts;
public EBike(int watts,int numWheels)
{
super(); // OK!!!
Super(numWheels); //Error!!!SuperClass无有参constructor
numOfWatts = watts;
}
public EBike(int watts)
{
numOfWatts = watts;// OK!Implicitly call java提供的父类
无参构造函数! }
public int getNumOfWatts()
Test Booklet
{ return numOfWatts; }
}
三、Method Overload(函数重载)
Method Overload即函数名相同,但是函数的参数个数不同,或者函数参
数个数相同,但是不同类型参数顺序不同。
Method Overload分为合法的重载和不合法的重载(即会报错!)
不合法的重载,通常是两个函数参数个数相同,但是相同类型的参数顺序不
同导致在实际调用函数时,并不能通过实际参数区分调用的到底是哪个函数。
比如:
以上两个函数再实际调用时,比如
前面两个参数都是String变量,并不能根据变量明确知道哪个该赋值给
bookTitle,哪个赋值给author,所以无法区分book1和book2实际调用的是哪
个bookConstructor,所以是无效的method overload;
有效的Method overload比如:
bookTitle分别是第一个第二个变量或者第一个第三个变量,所以是有效的
method Overload
Test Booklet
四.重点例题解析:
A. 父类new子类等:
public class Animal
{
public void eat()
I. a.eat();
II.a.roar();
(A) I only
(B) II only
对a进行了强制转换为子类就可以调用子类的函数roar()了
Test Booklet
public class C1
{
public C1()
{ /* implementation not shown */ }
public void m1()
{ System.out.print("A"); }
public void m2()
{ System.out.print("B"); }
}
}
The following code segment appears in a class other than C1 or C2.
The code segment is intended to produce the output AB. Which of the following best
explains why the code segment does not produce the intended output?
(A) A compile-time error occurs because obj1 is declared as type C1 but instantiated as type C2.
(B) A runtime error occurs because method m1 does not appear in C2.
(C) Method m1 is not executed because it does not appear in C2.
(D) Method m2 is executed from the subclass instead of the superclass because obj1 is
instantiated as a C2 object.
(E) Method m2 is executed twice (once in the subclass and once in the superclass) because it
appears in both classes.
obj1只能调用父类C1函数m1和m2, 调用没有问题。
此题说调用m1()和m2()后要打印”AB”但是打印的结果不对。
调用m1()时,只有父类有m1(),子类继承父类的m1(),所以表现父类的行为打印
A,调用m2()时,子类父类都有m2(),所以表现子类的m2()行为,打印的是C所
以选D, 子类的m2被执行,因为obj1被初始化为子类的object
Test Booklet
B. 子类中用super调用父类函数,表现一部分父类行为:
The following code segment appears in a class other than Ticket or DiscountTicket.
(A) 5.0
(B) 10.0
(C) Price is 5.0
(D) Price is 10.0
(E) There is no output because the code does not compile.
解析:选C,此题父类是Ticket t = new子类构造函数DicountTicket(10.0)
Test Booklet
System.out.println(t)默认调用的是toString函数
此题首先Ticket t = new子类构造函数DicountTicket(10.0)时调用子类的构造
函数
public DiscountTicket(double p)
{
super(p);
}
子类的构造函数调用了父类的有参构造函数,并将参数值10.0传给父类
public Ticket(double p)
{
price = p;
}
所以父类的price值为10.0
之后System.out.println(t)默认调用的是toString函数
public String toString()
{
return "Price is " + getPrice();
}
toString()在父类中定义且没有被子类改写,所以表现父类的行为
return "Price is " + getPrice();
toString()函数又调用了getPrice()函数,getPrice()在父类子类中都有,且t是
new子类构造函数新建的object,所以表现子类的行为所以会调用子类的
getPrice()函数即
public double getPrice()
{
return super.getPrice() / 2.0;
}
在子类的getPrice()函数中又通过super.调用了父类的getPrice()函数,并将父类
的getPrice()函数结果/2.0后返回,所以先看父类的getPrice()函数:
public double getPrice()
{
return price;
}
Test Booklet
直接返回price的值是一开始调子类构造函数初始化的10.0
返回结果
C. 父类子类constructor MCQ例题
instance variables
D. Correct
variables
statement
(A) I only
(B) II only
public A()
{
x = 0;
}
so x is 0 then y = 0, assigns 0 to y
II super(0) ; y = 0;
显式调用父类的有参构造函数
public A(int y)
{
x = y;
}
所以x =0,Then y = 0, 最终 x = 0 y= 0
Test Booklet
III
x = 0 is wrong, x is instance variable in superClass, cannot be
directly accessed in subClass
3. Consider the following class definitions.
// No constructor defined
private int
numBatteries;
numBatteries = batteries;
Bike or EBike.
Which of the following best describes the effect of executing the code
Test Booklet
segment?
instance variable
(A) numWheels. The instance variable numBatteries is initialized using
the value of the parameter
batteries.
An implicit call to the one-parameter Bike constructor with the
parameter passed to
(B) the EBike constructor initializes the instance variable numWheels.
The instance variable
numBatteries is initialized using the value of the parameter batteries.
Because super is not explicitly called from the EBike constructor, the
instance variable numWheels
(C) is not initialized. The instance variable numBatteries is initialized
using the value of the parameter
batteries.
(D) The code segment will not execute because the Bike class is a
(E) The code segment will not execute because the constructor of
答案是 A 此题父类没有任何构造函数,所以java会提供一个默认的无参
构造函数,因为子类也没有明确的super关键字调用父类的构造函数,所
以会隐式地调用父类的无参构造函数,选A